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查找字符串中第一个非0的位置截取

php查找字符串中第一个非0的位置截取

话不多说,请看代码: $str = '00000000000000000000000000000000000000001234506'; $preg = '/[0]*/'; $res...

php的XML文件解释类应用实例

本文实例讲述了php的XML文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具体如下: XMLParser.class.php类文件如下: <?php /...

屏蔽PHP默认设置中的Notice警告的方法

PHP的默认设置是显示Notice警告提示,这会造成页面无法正常显示出来。你有没定义的变量直接使用了。不过编PHP的时候本来就不像C++那么严格,编程的时候经常还会利用这个特点。 在把自...

php木马攻击防御之道

1、防止跳出web目录   首先修改httpd.conf,假如您只允许您的php脚本程式在web目录里操作,还能够修改httpd.conf文档限制php的操作路径。比如您的web目录是/...

PHP的加密方式及原理

复制代码 代码如下: <?php //变量注意区分数字 "0" 和 字符"O" $OOO000000=urldecode('%66%67%36%73%62%65%68%70%72%...