开发者

simple php image manipulation

开发者 https://www.devze.com 2023-01-16 07:16 出处:网络
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.

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 ima开发者_JAVA百科ge.


$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.


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消