php动态生成缩略图并输出显示的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php动态生成缩略图并输出显示的方法。分享给大家供大家参考。具体如下:

调用方法:

<img src="thumbs.php?filename=photo.jpg&width=100&height=100">

此代码可以为大图片动态生成缩略图显示,图片在内存中生成,不在硬盘生成真实文件

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程序设计有所帮助。

相关文章

深入理解PHP中的Session和Cookie

在一个页面设置一个cookie时,必须刷新或到下一个页面才可以用 $_COOKIE 得到变量的值.原因是因为当页面第一次被浏览器访问载入时,页面中的 cookie 会被设置,将其发送存储...

PHP设计模式之工厂模式与单例模式

本文实例讲述了PHP设计模式之工厂模式与单例模式实现方法。分享给大家供大家参考,具体如下: 设计模式简单说应对某类问题而设计的解决方式 工厂模式:应对需求创建相应的对象 class...

PHP创建单例后台进程的方法示例

本文实例讲述了PHP创建单例后台进程的方法。分享给大家供大家参考,具体如下: 可以通过如下语句启动一个PHP后台进程: $command = " php script.php ";...

php输出含有“#”字符串的方法

本文实例讲述了php输出含有“#”字符串的方法。分享给大家供大家参考,具体如下: 因为#在php中是注释,无法正常输出,需要转换和处理。 输出页: <? functi...

php及codeigniter使用session-cookie的方法(详解)

1、读写cookie <1>原生 setcookie('name','value',time) 设置失败,没有正常写入浏览器,测试失败,原因未知 <2>...