image resize problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

image resize problem

Post by calmchess »

I have the following code which resizes an image fine if i run the script from the command line using php debugger ...works 100% that way but if I run it from a webpage it doesn't do any thing ...why?

Code: Select all

<?php
// The file
$filein = 'chels3.jpg'; // File in
$fileout = time().'.jpg'; // Fileout - optional 
$imagethumbsize_w = 150; // thumbnail size (area cropped in middle of image)
$imagethumbsize_h = 150; // thumbnail size (area cropped in middle of image)
resize_then_crop( $filein,$fileout,$imagethumbsize_w,
$imagethumbsize_h,/*rgb*/"255","255","255");

function resize_then_crop( $filein,$fileout,$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue)
{

// Get new dimensions
list($width, $height) = getimagesize($filein);
$new_width = $width * $percent;
$new_height = $height * $percent;

   if(preg_match("/.jpg/i", "$filein"))
   {
       $format = 'image/jpeg';
   }
   if (preg_match("/.gif/i", "$filein"))
   {
       $format = 'image/gif';
   }
   if(preg_match("/.png/i", "$filein"))
   {
       $format = 'image/png';
   }
   
       switch($format)
       {
           case 'image/jpeg':
           $image = imagecreatefromjpeg($filein);
           break;
           case 'image/gif';
           $image = imagecreatefromgif($filein);
           break;
           case 'image/png':
           $image = imagecreatefrompng($filein);
           break;
       }

$width = $imagethumbsize_w ;
$height = $imagethumbsize_h ;
list($width_orig, $height_orig) = getimagesize($filein);

if ($width_orig < $height_orig) {
  $height = ($imagethumbsize_w / $width_orig) * $height_orig;
} else {
   $width = ($imagethumbsize_h / $height_orig) * $width_orig;
}

if ($width < $imagethumbsize_w)
//if the width is smaller than supplied thumbnail size 
{
$width = $imagethumbsize_w;
$height = ($imagethumbsize_w/ $width_orig) * $height_orig;;
}

if ($height < $imagethumbsize_h)
//if the height is smaller than supplied thumbnail size 
{
$height = $imagethumbsize_h;
$width = ($imagethumbsize_h / $height_orig) * $width_orig;
}

$thumb = imagecreatetruecolor($width , $height);  
$bgcolor = imagecolorallocate($thumb, $red, $green, $blue);  
ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor);
imagealphablending($thumb, true);

imagecopyresampled($thumb, $image, 0, 0, 0, 0,
$width, $height, $width_orig, $height_orig);
$thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h);
// true color for best quality
$bgcolor = imagecolorallocate($thumb2, $red, $green, $blue);  
ImageFilledRectangle($thumb2, 0, 0,
$imagethumbsize_w , $imagethumbsize_h , $white);
imagealphablending($thumb2, true);

$w1 =($width/2) - ($imagethumbsize_w/2);
$h1 = ($height/2) - ($imagethumbsize_h/2);

imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1,
$imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h);

// Output
//header('Content-type: image/gif');
//imagegif($thumb); //output to browser first image when testing

if ($fileout !="")imagejpeg($thumb2, $fileout); //write to file
header('Content-type: image/gif');
imagejpeg($thumb2); //output to browser
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How are you calling it from a web page?

Is there a reason why imagejpeg() is used for gif?
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

oh thats just a typo .....doesn't matter anyway it can use any formatte it wants.....anyway..... i just browse to bring up the webpage and nothing happens...anytime i do that with another php page everything works fine just not this one page its strange....however if i use the command line (php resize.php) then everything works properly i'm baffled by it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Comment out the header() call(s) then access it again and/or look in your error logs. I would suspect that the files you are referencing don't exist or cannot be read or written to.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

here is the first error which i fixed but then i get the second error in the apache file. but I don't get those errors at all if i run it from the command line.



[Tue Jan 16 23:43:40 2007] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined function: imagecreatefromjpeg() in C:\\webserver\\resize.php on line 38


[Tue Jan 16 23:44:38 2007] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined function: imagecreatetruecolor() in C:\\webserver\\resize.php on line 72
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

run phpinfo() to make sure gd is loaded and that it supports those image formats
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

I already did that and it is enabled.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

GD 1.8 or later?
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

GD 2.0
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

imagecreatetruecolor() needs GD 2.0.1 or later, but imagecreatefromjpeg() should be working if GD is enabled. I'm stumped...
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

yeah I have likd GD 2.0.38 or some such noise all that stuff is supported ....I know that because the script runs fine without any errors when i run it from the command line .....If i were to comment out php_gd2.dll in php.ini then it doesn't run at all so I think GD is working properly but there is something wrong with running it from a webpage.
Post Reply