By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

resizing a image

anfetienne
100+
P: 424
hi.....i have this script (below #1) that is linked to another php file SimpleImage.php (#2) im trying to get it to work on my uploaded images but it keeps coming up errors.....i haven't altered anything except for:

$image->load('picture.jpg');
$image->resize(546,403);
$image->save('picture2.jpg');

I put the variables i need in there within " " instead of ' ' and i always get a error message.....am i doing something or cant this be used to the scale i require


#1
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.    include('SimpleImage.php');
  3.    $image = new SimpleImage();
  4.    $image->load('picture.jpg');
  5.    $image->resize(546,403);
  6.    $image->save('picture2.jpg');
  7. ?>
  8.  
#2
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. * This program is free software; you can redistribute it and/or 
  9. * modify it under the terms of the GNU General Public License 
  10. * as published by the Free Software Foundation; either version 2 
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful, 
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  15. * GNU General Public License for more details: 
  16. * http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19.  
  20. class SimpleImage {
  21.  
  22.    var $image;
  23.    var $image_type;
  24.  
  25.    function load($filename) {
  26.       $image_info = getimagesize($filename);
  27.       $this->image_type = $image_info[2];
  28.       if( $this->image_type == IMAGETYPE_JPEG ) {
  29.          $this->image = imagecreatefromjpeg($filename);
  30.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  31.          $this->image = imagecreatefromgif($filename);
  32.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  33.          $this->image = imagecreatefrompng($filename);
  34.       }
  35.    }
  36.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  37.       if( $image_type == IMAGETYPE_JPEG ) {
  38.          imagejpeg($this->image,$filename,$compression);
  39.       } elseif( $image_type == IMAGETYPE_GIF ) {
  40.          imagegif($this->image,$filename);         
  41.       } elseif( $image_type == IMAGETYPE_PNG ) {
  42.          imagepng($this->image,$filename);
  43.       }   
  44.       if( $permissions != null) {
  45.          chmod($filename,$permissions);
  46.       }
  47.    }
  48.    function output($image_type=IMAGETYPE_JPEG) {
  49.       if( $image_type == IMAGETYPE_JPEG ) {
  50.          imagejpeg($this->image);
  51.       } elseif( $image_type == IMAGETYPE_GIF ) {
  52.          imagegif($this->image);         
  53.       } elseif( $image_type == IMAGETYPE_PNG ) {
  54.          imagepng($this->image);
  55.       }   
  56.    }
  57.    function getWidth() {
  58.       return imagesx($this->image);
  59.    }
  60.    function getHeight() {
  61.       return imagesy($this->image);
  62.    }
  63.    function resizeToHeight($height) {
  64.       $ratio = $height / $this->getHeight();
  65.       $width = $this->getWidth() * $ratio;
  66.       $this->resize($width,$height);
  67.    }
  68.    function resizeToWidth($width) {
  69.       $ratio = $width / $this->getWidth();
  70.       $height = $this->getheight() * $ratio;
  71.       $this->resize($width,$height);
  72.    }
  73.    function scale($scale) {
  74.       $width = $this->getWidth() * $scale/100;
  75.       $height = $this->getheight() * $scale/100; 
  76.       $this->resize($width,$height);
  77.    }
  78.    function resize($width,$height) {
  79.       $new_image = imagecreatetruecolor($width, $height);
  80.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  81.       $this->image = $new_image;   
  82.    }      
  83. }
  84. ?>
  85.  
  86.  
  87.  
  88.  
Mar 7 '09 #1
Share this Question
Share on Google+
14 Replies


Markus
Expert 5K+
P: 6,050
The error is?
Mar 7 '09 #2

anfetienne
100+
P: 424
well.....the error has stopped but it said that this was invalid

function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);

and it wasn't detecting the files i was pointing to with my variable....this is

$resizeM = "upload/imgtemp/m/$random_digit/$filename";

include('SimpleImage.php');
$image = new SimpleImage();
$image->load("$resizeM");
$image->resize(250,400);
$image->save("$resizeM");
Mar 7 '09 #3

anfetienne
100+
P: 424
thats the changes ive made
Mar 7 '09 #4

P: 88
Looks like a problem with the path try:

Expand|Select|Wrap|Line Numbers
  1. echo $resizeM;
And see what it outputs.

Also where is that script in relation to the uploads/ directory?
Mar 8 '09 #5

Markus
Expert 5K+
P: 6,050
The error has now stopped? Is the image resizing working now then?
Mar 8 '09 #6

anfetienne
100+
P: 424
no it isn't resizing but it isn't giving me any errors.....its weird.....when i echo $resizeM i get the full location to the images that need resizing.

i'm going to think abaout this one and post again soon
Mar 9 '09 #7

Markus
Expert 5K+
P: 6,050
Do you have error reporting on?

Nothing jumps out at me, but could you be uploading an image that isn't a JPEG, PNG or GIF?
Mar 9 '09 #8

anfetienne
100+
P: 424
error reporting is on and i have a set of test jpeg's that i am using to upload.

it's strange how it comes up with no errors at all but doesnt resize anything. I can't seem to figure this out

would it be better or easier to try resize the images during the upload process?
Mar 10 '09 #9

anfetienne
100+
P: 424
this is the coding i made to upload my files with.....can it be adapted to resize the images on upload?

Expand|Select|Wrap|Line Numbers
  1. <?
  2. while(list($key,$value) = each($_FILES['images']['name']))
  3.       {
  4.          if(!empty($value))
  5.          {
  6.             $filename = $value;
  7.                $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  8.  
  9.                $add = "upload/imgtemp/m/$random_digit/$filename";
  10.                $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  11.                        //echo $_FILES['images']['type'][$key];
  12.               // echo "<br>";
  13.                copy($_FILES['images']['tmp_name'][$key], $add);
  14.                chmod("$add",0777);
  15.                copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  16.                chmod("$tempFLDR",0777);
  17.  
  18. $folderPath="upload/$random_digit/";
  19.          }
  20.       }
  21. ?>
  22.  
Mar 10 '09 #10

anfetienne
100+
P: 424
wooohoooo i have got it to resize lol but errmmmm if i upload 1 image then it resizes fine but if i upload more than one image it only resizes the last image.

why does it do this....here is the code i have that is working for me

Expand|Select|Wrap|Line Numbers
  1. <?
  2. while(list($key,$value) = each($_FILES['images']['name']))
  3.         {
  4.             if(!empty($value))
  5.             {
  6.                 $filename = $value;
  7.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  8.  
  9.                     $add = "upload/imgtemp/m/$random_digit/$filename";
  10.                     $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  11.                        //echo $_FILES['images']['type'][$key];
  12.                  // echo "<br>";
  13.                     copy($_FILES['images']['tmp_name'][$key], $add);
  14.                     chmod("$add",0777);
  15.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  16.                     chmod("$tempFLDR",0777);
  17.  
  18. $folderPath="upload/$random_digit/";
  19.             }
  20.         }
  21. include('SimpleImage.php');
  22. $image = new SimpleImage();
  23. $image->load("$add");
  24. $image->resize(250,400);
  25. $image->save("$add"); 
  26. ?>
  27.  
Mar 10 '09 #11

Markus
Expert 5K+
P: 6,050
That's because your image manipulation happens outside of the while loop, meaning only the last image's path will be read. Move the include to the beginning of the file, and move the method calls to the end of the while loop.
Mar 10 '09 #12

anfetienne
100+
P: 424
like this? im still a newbie....sorry for the hassle

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include('SimpleImage.php');
  3. while(list($key,$value) = each($_FILES['images']['name']))
  4.         {
  5.             if(!empty($value))
  6.             {
  7.                 $filename = $value;
  8.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  9.  
  10.                     $add = "upload/imgtemp/m/$random_digit/$filename";
  11.                     $tempFLDR = "upload/imgtemp/t/$random_digit/$filename";
  12.                        //echo $_FILES['images']['type'][$key];
  13.                  // echo "<br>";
  14.                     copy($_FILES['images']['tmp_name'][$key], $add);
  15.                     chmod("$add",0777);
  16.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  17.                     chmod("$tempFLDR",0777);
  18.  
  19. $folderPath="upload/$random_digit/";
  20.             }
  21. $image = new SimpleImage();
  22. $image->load("$add");
  23. $image->resize(546,403);
  24. $image->save("$add"); 
  25.         }
  26.  
  27. ?>
  28.  
Mar 10 '09 #13

anfetienne
100+
P: 424
Thanks Markus!!!!! it works like a charm...i'll be back with a link once the finished product is done.

Thanks again
Mar 10 '09 #14

Markus
Expert 5K+
P: 6,050
@anfetienne
Coolios, and you're welcome.

You don't need to recreate the object ($x = new x;) in the while loop - that'll use up more resources. So, just create the object after you include the class.
Mar 11 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.