Link to home
Create AccountLog in
Avatar of allanch08
allanch08Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP imagejpeg not working

Happy New Year experts!

I'm trying to resize images using PHP if they exist. I've adapted part of a script already on EE but when I output the image I get JPEG text. Any help on resolving this would be appreciated!
<?php // Check image size.
$imagesize = @getimagesize($product["image_url"]);
if (!$imagesize){
	$productimage = "/images/noimages.gif";
}
else
{
	$width = 160;
	$productimage = $product["image_url"];
	$source = imagecreatefromjpeg("$productimage");
	$imageX = imagesx($source);
	$imageY = imagesy($source);
 
	if ($imageX > $width) {
		$tnailX = $width;
		$tnailY = (int) (($tnailX * $imageY) / $imageX);
	} else {
		$tnailX	= $imageX;
		$tnailY = (int) (($tnailX * $imageY) / $imageX);
	}

	$target = imagecreatetruecolor($tnailX, $tnailY);
	imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
	header('Content-Type: image/jpeg');
	imagejpeg($target, NULL, 100);
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of allanch08

ASKER

thanks, can't believe I didn't notice that. Corrected it but still have the same problem.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
How are you calling this routine?
Hi everyone, the routine is part of a search results page. All results are printed along with corresponding images. The images exist but I'd like to resize them as they are different dimensions. The above code is sanwiched between a foreach loop. I'll introduce more checks as suggested Ray. thanks
"Probably you're having this issue because there is more to the script than you posted here?  Something else is sending browser output before the header() on line 24."

Does this mean that 'header('Content-Type: image/jpeg');' has to be used at the top of the script before any html is ouputted?
Avatar of kawzaki
kawzaki

Hello,

you have a GIF but you want to use imageCreateFromJpeg()
so that will result in an ERROR.

make sure you pass the right type to the right function.


i have tested the code, it is working fine with JPG images.
Hi, the gif is just for that specific image. all over images are jpg.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
okay, so i should do as Ray suggested - make it a batch process and wirte files to a directory.
Can you attach one of the image you have, and i will test it here.

I had a similar problem and spent days trying to figure out the problem, finally found it was due to the image renaming. Many users have png images saved as jpg. Thus resulting in error when using the wrong type of imagecreatefrom....()
hello, attached is one of the origianl image files. User generated image
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
hi, thanks for your help, thats helps the noimages.gif bit.

if i add this

imagejpeg($target, 'test.jpg', 100);
echo '<img src="test.jpg">';

from line 24 I get images but only the first one printed across them all.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
please show us the way you are calling this script in your code..
the logic or pseudo code.. cause it is obvious the problem has to do with passing the image name to be resized.

Hi Ray, I also had a problem with the PNG_ALL_FILTERS:

Use of undefined constant PNG_ALL_FILTERS - assumed 'PNG_ALL_FILTERS

thanks
This is the code I have so far that can can display images. However, it only displays the first image repeatedly.
<?php
	if (file_exists("html/user_searchresults_before.php")) require("html/user_searchresults_before.php");
	$columns = 4;
	$currentColumn = 0;
?>

<div class='searchresults'>
    <?php foreach($searchresults["products"] as $product): ?>
	<div class='box'>
		<div class='boximage'>
			<?php // Check image size.
			error_reporting(E_ALL);
			if (!empty($product["image_url"]) && $product["image_url"] != 'noimage.gif') {
				$width = 160;
				$source = $product["image_url"];
				$source = imagecreatefromjpeg($source);
				$sourcex = imagesx($source);
				$sourcey = imagesy($source);

				if ($sourcex > $width) {
					$tnailx = $width;
					$tnaily = (int) (($tnailx * $sourcey) / $sourcex);
				} else {
					$tnailx	= $imagex;
					$tnaily = (int) (($tnailx * $sourcey) / $sourcex);
				}
				$target = imagecreatetruecolor($tnailx, $tnaily);
				imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailx, $tnaily, $sourcex, $sourcey);
				imagejpeg($target, 'testimage.jpg', 100);
				echo '<img src="testimage.jpg">';
				imagedestroy($source);
				imagedestroy($target);
			} else {
				echo '<img src="/images/noimage.gif">';
			}
			?>
		</div>
	<?php endforeach; ?>
	<br class="clear-float" />
</div>

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks Ray your help is always appreciated. My original question was to get the script I printed to work and with some help I've managed to get it to do so partially. Your script works but I've tried to implement it into my existing script where it doens't work.

My whole script consists of several parts - header, menu, searchresults (this part), navigation, footer. All these parts are separate php files that are called together under search.php. header.php already has Content-Type: text/html;charset="utf-8". If I try to implement your script I get

Warning: Cannot modify header information - headers already sent...

If I remove header('Content-type: image/png'); then I get all the funny PNG text. So I know its getting some kind of image. The script I printed works but only the first image is repeated over again and I'm guessing you're right when you say 'line 29 and 30 that keeps overwriting the same image file.' but I can't figure out a way around it at the moment. Thanks
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thanks, I get the image fine, but when I adapt it for my script even though $src, $dst_wide, $dst_high changes with each image in the foreach loop the printed image is always the first one. I've gone through each step and all is fine with variables. so something is up with the target file 'RAY_temp_png.png' .
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I agree with Ray Paseur,
the original issue in this question was about imagejpeg() not working, and as tested, the resize image code is working fine. it's the part of how to utilize the function and embed it into your project.

good luck


thanks for the help everyone, closing question as advised