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常用函数和常见疑难问题解答

首先介绍下比较简单但必不可少且实用的知识,可以当手册查询,适合像我一样的新手看。 PHP常用库函数介绍 一、PHP字符串操作常用函数1.确定字符串长度 int strlen(string...

PHP输出数组中重名的元素的几种处理方法

1.可以直接用php的内置函数array_intersect() array array_intersect ( array $array1 , array $array2 [, arr...

php实现用于计算执行时间的类实例

本文实例讲述了php实现用于计算执行时间的类。分享给大家供大家参考。具体如下: 有了这个php类,计算函数或者一段代码的执行时间就简单了 <?php class c_...

Yii 2.0如何使用页面缓存方法示例

前言 本文主要给大家介绍的是关于Yii2.0如何使用页面缓存的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍。 起初使用页面缓存,发现使用于含有参数的方法存在弊端,只能缓存第...

php判断变量类型常用方法

现在让我们了解下具体的使用方法 gettype() gettype 会根据 参数类型返回下列值 “boolean”(从 PHP 4 起) “integer” “double”(如果是 f...