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

yipeiwu_com4年前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 JWT在web端中的使用方法教程

php JWT在web端中的使用方法教程

解释一下JWT JWT就是一个字符串,经过加密处理与校验处理的字符串,由三个部分组成。基于token的身份验证可以替代传统的cookie+session身份验证方法。三个部分分别如下:...

PHP控制网页过期时间的代码

当然,前提要先打开CDN中一个功能reload_into_ims on.这样用户发送过来no-cache也不怕了.因为这样会给给no-cache转成If-Modified-Since ....

linux下为php添加iconv模块的方法

./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --w...

php通过strpos查找字符串出现位置的方法

本文实例讲述了php通过strpos查找字符串出现位置的方法。分享给大家供大家参考。具体分析如下: strpos用来查找一个字符串在另一个字符串中首次出现的位置,strpos区分大小写,...

PHP 一个页面执行时间类代码

核心代码 <?php class Timer//页面执行时间类 { var starttime;//页面开始执行时间 var stoptime;//页面结束执行...