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 //--调用方法/demo.php?url=http://v.youku.com/v_show/id_XMzkyODA2NTEy.html echo...

shopex中集成的站长统计功能的代码简单分析

复制代码 代码如下: <?php //我们的域名,这里可以不唯一的 $domain = 'localhost'; //这个应该是CNZZ授权给shopex的加密密钥,如果错了就不能...

php curl 获取https请求的2种方法

今天一个同事反映,使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Detail...

php实现网页上一页下一页翻页过程详解

php实现网页上一页下一页翻页过程详解

前言 这几天做项目因为数据太多,需要对信息进行上下翻页展示,就自己写了翻页的代码 大致功能就是页面只显示几条信息,按上一页、下一页切换内容,当显示第一页时上一页和首页选项不可选,当页面加...

PHP中对汉字进行unicode编码和解码的实现方法(必看)

实例如下: //将内容进行UNICODE编码 function unicode_encode($name) { $name = iconv('UTF-8', 'UCS-2', $...