PHP动态生成缩略图方法介绍
本例详细介绍了使用PHP动态生成缩略图并展示的具体实现方式,供开发者参考借鉴。
具体调用步骤如下:
该方案支持对大图进行实时缩略图生成,所有图像处理均在内存中完成,不会在服务器硬盘上产生实际的物理文件。
核心逻辑封装于thumbs.php文件中,内容如下:
<?php
$filename= $_GET['filename'];
$width = $_GET['width'];
$height = $_GET['height'];
$path="http://localhost/images/"; //finish in "/"
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($path.$filename);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($path.$filename);
imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
// Output
imagejpeg($image_p, null, 100);
// Imagedestroy
imagedestroy ($image_p);
?>
期望上述内容能为您的PHP开发工作提供实用帮助。