php动态生成缩略图并输出显示的方法

yipeiwu_com5年前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+Ajax处理xml与json格式数据的方法示例

本文实例讲述了php+Ajax处理xml与json格式数据的方法。分享给大家供大家参考,具体如下: 一、ajax如何处理xml数据格式 register.php 只需修改上一篇《php+...

PHP中的随机性 你觉得自己幸运吗?

PHP中的随机性 你觉得自己幸运吗?

本文分析了生成用于加密的随机数的相关问题。 PHP 5没有提供一种简单的机制来生成密码学上强壮的随机数,但是PHP 7通过引入几个CSPRNG函数来解决了这个问题。 一、什么是CSPR...

php生成无限栏目树

栏目数组: $arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0), Array('cid' =&...

php阳历转农历优化版

本文实例为大家分享了php阳历转农历代码,供大家参考,具体内容如下 <?php function nongli($riqi) { //优化修改 20160807 F...

一些关于PHP的知识

1、如何配置PhpMyAdmin2.9 网络上很多教程的配置文件是针对PhpMyAdmin底版本的,一开始连2.9配置文件都不知道放哪里? 配置文件相对地址是:config.sample...