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和js如何通过json互相传递数据相关问题探讨

当我们在结合php和javascript实现某些功能时,经常会用到json。json是js的一种数据格式,可以直接被js解析。而php无法直接读取json数据,但是php提供了json_...

非常全面的php日期时间运算汇总

实例讲解之前,先来介绍几个核心函数:  mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有...

PHP 使用header函数设置HTTP头的示例解析 表头

如下所示:复制代码 代码如下://定义编码  header( 'Content-Type:text/html;charset=utf-8 ');  //Atom&nb...

PHP判断上传文件类型的解决办法

分享给大家php判断上传文件类型的方法,大家一起学习学习。 /** * 读取文件前几个字节 判断文件类型 * @return String */ function check...

浅谈socket同步和异步、阻塞和非阻塞、I/O模型

在进行网络编程时,常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式 同步/异步主要针对C端: 同步:c端发出一个功能调用时,在没有...