PDA

View Full Version : Need options for resizing large images


JohnDubya
05-11-2007, 09:50 PM
I have a script that allows the user to upload a jpg, png, or gif image. I have it limited to a 1MB upload and only files with the three extensions I mentioned. Now, I need to figure out how to only allow those images to display at 175 pixels wide. I wrote up a quick function to calculate the ratio between the width and height and then throw those new width/height values into the <img> tag, but the image looks all garbled because it was shrunk so much by the browser.

So now, I'm trying to figure out if my best option would be to use imagecopyresampled() or similar on the page where I view the images, or if I should use imagecopyresampled() on the page where the user uploads the image. Should I check the image when the user uploads it and do the resize there? I didn't want to do that just in case I ever wanted the image to display bigger. What are my options here?

printf
05-11-2007, 10:09 PM
Just create a thumbnail of each image at upload time, it's better to do that than to create the thumbnail every time it is to be displayed. So keep the original and make a thumb of the original, so if the user want to see the full size image you can link it to the thumbnail image, by just surrounding the thumbnail image with <a> tag or some JavaScript onClick event called function!

JohnDubya
05-11-2007, 10:15 PM
Awesome, I didn't even think of creating a thumbnail. Thanks for the idea!

JohnDubya
05-14-2007, 07:18 PM
Ok, I've run into another problem. I have also allowed my users to input a URL to an image. getimagesize() works for off-site images, so I can at least get the width and height to see if the image needs shrinking. But since I probably don't want to save the thumbnail, how can I make the off-site image shrink down to a maximum of 200px? As I said before, I tried doing it with just calculating the new width and height and putting it in the <img> tag, but it looked all pixelated. Can I do what I'm needing with the GD functions?

rafiki
05-15-2007, 11:40 AM
would there be a way of saving it to your server then creating the thumb nail automatically?

<?php
function http_get_file($url) {

$url_stuff = parse_url($url);
$port = isset($url_stuff[‘port’]) ? $url_stuff[‘port’]:80;

$fp = fsockopen($url_stuff[‘host’], $port);

$query = ‘GET ‘ . $url_stuff[‘path’] . ” HTTP/1.0\n“;
$query .= ‘Host: ‘ . $url_stuff[‘host’];
$query .= “\n\n“;

fwrite($fp, $query);

while ($line = fread($fp, 1024)) {
$buffer .= $line;
}

preg_match(‘/Content-Length: ([0-9]+)/’, $buffer, $parts);
return substr($buffer, - $parts[1]);
}
?>
i found this function which claims it can save file from URL

mlseim
05-15-2007, 01:49 PM
John,

Give us a URL to a large image and I'll see what PHP GD can do with it,
and I can then show the script. Give a link to an actual example of one
of your images.

JohnDubya
05-15-2007, 03:45 PM
John,

Give us a URL to a large image and I'll see what PHP GD can do with it,
and I can then show the script. Give a link to an actual example of one
of your images.

Here's one from Amazon:

http://ec1.images-amazon.com/images/I/51ND6PMJVQL._AA240_.jpg

This one has a width of 240, so my thumbnail script I have now would shrink it proportionately to a width of 200. I just need to know how to get the off-site image file and save it locally. Thanks for your offers to help!

JohnDubya
05-15-2007, 04:47 PM
rafiki, I've been jackin around with that function, but I can't get it to do anything helpful. I tried implementing it into my thumbnail script, but I guess I'm probably not totally sure how it works. What exactly is it returning for me to use?

mlseim
05-15-2007, 05:15 PM
John ...

Test it out ....

This is the HTML file, call it "test.php" (or test.html ... whatever):

<html>
<head><title>Image Resize Test</title>
</head>
<body>
<img src="resize.php?pic=http://ec1.images-amazon.com/images/I/51ND6PMJVQL._AA240_.jpg" alt="" />
</body>
</html>


This is the PHP script called "resize.php" ...

<?php

// The file
$filename=$_GET['pic'];

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
imagedestroy($image_p);
?>


Here's my working test:
http://www.cgrelayforlife.com/test.php

EDIT:
I forgot about the part of saving it locally ...

Instead of displaying image, it can be saved
with the "imagejpeg" function:

$file_loc = "../images/myphotos/".$imageName.".jpg";
imagejpeg($image_p,$file_loc,100);




.

JohnDubya
05-15-2007, 08:43 PM
You're a stinkin genius!!! *gives mlseim a hand clap* Had to do some testing and little changes, but it works like a charm! Thank you so much!!

Just for reference, I built in a switch to check each file to see if its extension was jpg, png, or gif, like so:


//Look for the last period in the filename and get what's after it
$period = strrpos($filename, '.');
$start = ++$period;
$extension = substr($filename, $start);

// Content type
switch ($extension) {
case 'jpg':
header('Content-type: image/jpeg');
$image = imagecreatefromjpeg($filename);
break;
case 'png':
header('Content-type: image/png');
$image = imagecreatefrompng($filename);
break;
case 'gif':
header('Content-type: image/gif');
$image = imagecreatefromgif($filename);
break;
}


//Then, for output, do another switch to output the correct type of image
switch ($extension) {
case 'jpg':
imagejpeg($image_p, null, 100);
break;
case 'png':
imagepng($image_p, null, 100);
break;
case 'gif':
imagegif($image_p, null, 100);
break;
}

Thanks so much again! :thumbsup:

mlseim
05-16-2007, 12:16 AM
John ...

I'll take credit for showing it to you, but this is actually
where it came from ... along with detailed explanations
and examples of ALL the PHP GD functions:

http://us.php.net/manual/en/function.imagecopyresized.php

You're right on track with "customizing" (or tweaking) scripts for
your own use ... that's what learning PHP is all about.
It's very addicting to say the least.

JohnDubya
05-16-2007, 01:37 AM
Yeah, I had read up on some tutorials on how to mess with images with PHP, but I was having trouble understanding some things about them. After hours of testing and testing and testing, I finally got a script to work that makes thumbnails for images that my users upload, but I was just having trouble with off-site images. And now that's fixed too!

I've learned (mostly through this forum) to always try and Google and test and search FIRST...then come here and post. And even after posting, I always test and test and test more! It is quite addicting, I will admit. That's why I'm glad I got a job doing it! lol Getting paid to code is fun!:)