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 empty()与isset()区别的详细介绍

在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset...

解析thinkphp中的导入文件标签

第一个是import标签 ,导入方式采用类似ThinkPHP的import函数的命名空间方式,例如: import标签(采用命名空间方式引入资源文件) 闭合 闭合标签...

php自动识别文件编码并转换为UTF-8的方法

PHP转换文件编码是一个比较简单的事情,但是在开发中传递中文参数的时候,有时候不知道是什么编码,结果造成了乱码的现象。这里有个非常方便的解决办法,可以自动识别编码并转换为UTF-8。具体...

PHP的引用详解

引用是什么 在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以...

php输出表格的实现代码(修正版)

网上的代码很多都是错误的,【宜配屋www.yipeiwu.com】特修正了下。复制代码 代码如下:<html> <head> <title>二行5列一...