views:

44

answers:

2

I'm a beginner at this. And I tried to search on the internet about this. And almost all that I found requires frameworks and libraries. And I don't really know how to use frameworks. Can you recommend something that could help me go about image manipulation in php. Something for beginners like me. All I want to do for now is to output a thumbnail of 700 x 468 image. Without having the need to save the resized image.

+3  A: 
$width=700;
$height=468;

$image=imagecreatefromstring(file_get_contents($file));
$thumb=imagecreatetruecolor($width,$height);
imagecopyresampled($thumb,$image,0,0,0,0,$width/4,$height/4,$width,$height);
header('Content-Type: image/png');
imagepng($thumb);

This creates a thumbnail that's 1/4 the size of the original, without destroying image proportions. Although you don't need to save any thumbnail image, image manipulation gobbles up huge amounts of RAM. Make sure you always have enough.

stillstanding
should be noted that while you dont need a framework you have to have an image manipulation extension installed, this example uses GD. There is also ImageMagick. GD is more widely installed on shared hosts ive found, but ImageMagick seems to have a better feature set.
prodigitalson
+1  A: 

It's usually a good practice to save the thumbnail on the disk, for caching purpose, but if you really don't want it, just generate the thumbnail on the fly as explained by stillstanding, with the HTML img pointing to a php script like this:

<img src="resizeimg.php?img=original.jpg" width="700px" height="468px"/>

Additionnaly, you can make it invisible, with a bit of URL rewriting like this:

HTML

<img src="thumbnail-original.jpg" width="700px" height="468px"/>

.htaccess

RewriteRule ^thumbnail-(.*)$         resizeimg.php?img=$1         [L]
Damien