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

Interesting Image Problem

P: n/a
Hi

This one's been puzzling me for a while - I can only think it must be a
bug in PHP 5.1.4 (which is what I'm running). I'm pretty sure it does
the same thing in PHP 4. Is it a 'feature' I'm not aware of?

What I want is an image uploading program, which automatically saves a
fullsize version of the image, a thumbnail, and resizes the image
itself so it is no more than 400x400. So - three copies of the image:

Original image (resized)
Copy of original image (fullsize)
Thumbnail (100x100)

I then wanted to display the original image to show it had been
uploaded correctly.

I wrote a class that does this, but it wasn't working correctly. It
created all three images correctly, but when I output the original
image (which I had resized) it kept overwriting the fullsize image with
the resized image. I tracked the bug down to the header() call.

Here is a test page of what I want to do - to make sure it wasn't my
class being weird, I wrote this:

<?php

// get the original image details etc
$image_path = "E:\Eclipse\htdocs\core\outlineb.gif";

$im = imagecreatefromgif($image_path);

$tmp = getimagesize($image_path);
$width = $tmp[0];
$height = $tmp[1];

// create the second image
$new_image_path = str_replace(".","_fullsize.",$image_path);

copy($image_path,$new_image_path);
$im_two = imagecreatefromgif($new_image_path);

#imagedestroy($im_two);

// resize the original image
$im_resized = imagecreatetruecolor(100,100);
imagecopyresampled($im_resized,$im,0,0,0,0,100,100 ,$width,$height);

// save the resized image
imagegif($im_resized,$image_path);

// show the resized image
#header("content-type: image/gif");
imagegif($im_resized);

?>

You will see two commented lines. I tried destroying the resource
linking to the fullsize image before outputting the original image, but
that didn't work.

The second comment is the fascinating one. If you comment out the
header call, the three images are left resized correctly, and the
browser displays gobbldegook (obviously).

Uncomment the header call and the image is displayed correctly, but the
second image is overwritten. What's up with the header call? What is
making PHP do this?

Cheers

-bcg-

Sep 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
NC
Mr Fred wrote:
>
I wrote a class that does this, but it wasn't working
correctly. It created all three images correctly, but
when I output the original image (which I had resized)
it kept overwriting the fullsize image with the resized
image.
But this is exactly what you wrote:
$image_path = "E:\Eclipse\htdocs\core\outlineb.gif";
$im = imagecreatefromgif($image_path);
....
$new_image_path = str_replace(".","_fullsize.",$image_path);
....
imagegif($im_resized,$image_path);
First, you create $im from $image_path, then you save $im_resized to
$image_path instead of saving it to $new_image_path. Rewrite the last
line:

imagegif($im_resized, $new_image_path);

Cheers,
NC

Sep 8 '06 #2

P: n/a
Hi

Thanks for your comments.

The purpose of the script is to save the original image as
image_fullsize, and then resize the original image so it is no bigger
than 400x400 (or whatever).

The reason is this: on a website photo gallery, I want three images: a
thumbnail, a 400x400 max. image, and the fullsize image.

So the script saves the original image as image_fullsize, then resizes
the original image and saves it.

What happens though, is that the fullsize image gets resized as well -
but ONLY if you call the header() function.

bcg

Sep 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.