imagejpeg or imagecreatetruecolor

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
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

imagejpeg or imagecreatetruecolor

Post by modplod »

Hi all,

for some reason the code below is not writing the image info to the destination file, I have been trying for hours to get it to work, I have check all the vars and they are all being send and all have values, I had it working yesterday, but for some reason I'm missing the spark.

Code: Select all

function makeThumbnailImage($pathToSourceImage,$pathToThumbImage, $thumbHeight, $thumbWidth)
	{
		// get image type
		$imageType = $this->imageis($pathToSourceImage);
		if( $imageType == "jpg"){
			
			// create  output file
			$src_img=imagecreatefromjpeg($pathToSourceImage);
		
			// calutlate the size of out put file, maintaining aspect ratio
			$thumbXY = $this->calcNewSize($imageWidth,$imageHeight,$thumbWidth,$thumbHeight);
			
			$dst_img = @imagecreatetruecolor($thumbXY[0],$thumbXY[1]) or die("Unable to create thumbnail image stream!!<br>Image Name: $fileName");
			
			// write image to Thumb file
			imagejpeg($dst_img,$pathToThumbImage);
			
			// remove image from ram
			imagedestroy($dst_img);
			imagedestroy($src_img);

		}
	}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

does removing the error suppressant "@" give you an error?
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

Jcart wrote:does removing the error suppressant "@" give you an error?
no it does not.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Lets start with the more obvious then, what does

Code: Select all

echo $imageType;
return?
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

It returns a string representing the type of image, in this case all images ARE jpg and

Code: Select all

echo $imageType;
returns "jpg" in this case. gif for gif files, png for png files ect


I think the problem is within this part of the code, but not sure

Code: Select all

// calutlate the size of out put file, maintaining aspect ratio
            $thumbXY = $this->calcNewSize($imageWidth,$imageHeight,$thumbWidth,$thumbHeight);
            
            $dst_img = @imagecreatetruecolor($thumbXY[0],$thumbXY[1]) or die("Unable to create thumbnail image stream!!<br>Image Name: $fileName");
            
            // write image to Thumb file
            imagejpeg($dst_img,$pathToThumbImage);
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

ahaaaa.. I shouldtry this rewrting mu function from scratch more often:)

I seemed to have missed an entire step in the process

anyhow heres my finnished function

Code: Select all

function makeThumbnailImage($pathToSourceImage,$pathToThumbImage, $thumbHeight, $thumbWidth)
	{
		// get image type
		$imageType = $this->imageis($pathToSourceImage);
		if( $imageType == "jpg"){

			// get source image dimentions
			$img_details = getimagesize($pathToSourceImage);

			// calutlate the size of out put file, maintaining aspect ratio
			$thumbXY = $this->calcNewSize($img_details[0],$img_details[1],$thumbWidth,$thumbHeight);

			$dst_img= imagecreatetruecolor($thumbXY[0],$thumbXY[1]);
			
			$src_img = imagecreatefromjpeg($pathToSourceImage);
			
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumbXY[0],$thumbXY[1],$img_details[0],$img_details[1]);
			// write image to Thumb file
			imagejpeg($dst_img,$pathToThumbImage);

			// remove image from ram
			imagedestroy($dst_img);
			imagedestroy($src_img);
		}
	}
Post Reply