当前位置:首页 > PHP > 正文内容

PHP生成缩略图

高老师8年前 (2017-07-02)PHP1639
//参数1  文件名    参数2  缩放比例
 function   _thumb($_filename,$_percent){
  
ob_clean();
//生成png标头文件
header('Content-type:image/png');
 
$_n=explode('.', $_filename);
 
//获取文件的信息,宽和高
list($_width,$_height)=getimagesize($_filename);
 
//生成缩略后的大小
$_new_width=$_width*$_percent;
$_new_height=$_height*$_percent;
 
 
//按照新的大小创建微缩画布
$_new_image=imagecreatetruecolor($_new_width, $_new_height);
 
//按照已经有的图片创建一个画布[按照图片类型]
switch($_n[1]){
case 'jpg':$_image=imagecreatefromjpeg($_filename);
    break;
case 'png':$_image=imagecreatefrompng($_filename);
    break; 
case 'gif':$_image=imagecreatefromgif($_filename);
    break; 
}
 
//将原来的图复制到新的图片上
imagecopyresampled($_new_image, $_image, 0, 0, 0, 0, $_new_width, $_new_height, $_width, $_height);
 
//输出图片
imagepng($_new_image);
 
//销毁新资源句柄
imagedestroy($_new_image);
//销毁原资源句柄
imagedestroy($_image);
 
 }

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/28.html

分享给朋友:

“PHP生成缩略图” 的相关文章

php引用变量的完全理解

php引用变量的完全理解

在PHP中,大部分变量类型,如字符串,整型,浮点,数组等都是值类型的,而类和对象是引用类型.和其他语言有点差距.(1).在值类型中我们直接使用&符号表示指向对应变量的内存地址,当前变量和被指向的变量只要有1个的值被修改都会直接影响另外一个变量的值发生变化。(ps:还是非常节省内存的,可以使用...

php解决浮点数精度问题

php解决浮点数精度问题

首先看看以下代码:代码1:<?php $a=0.1; $b=0.7; if($a+$b==0.8) { echo "1"; } else{ echo "2"; } ?>代码2:<?php   &n...

php finally使用

php finally使用

<?php /**  * @throws Exception  */ function curl() {     throw  new \Exception('err...

posix_ttyname函数详解

posix_ttyname函数详解

posix_ttyname - 获取当前终端设备名称。<?php     var_dump( posix_ttyname(STDOUT) );我们启动一个终端,执行上面的代码输出:/dev/tty1我们再启动一个终端,执行上面的代码输...

PHP Warning:  ftok(): Project identifier is invalid

PHP Warning: ftok(): Project identifier is invalid

在使用ftok生成ipc进程通信key尝试将第二个参数项目标识符传入字符串报错:PHP Warning:  ftok(): Project identifier is invalid,查阅资料发现第二个字符串只能是1个字符串,长度为1....

PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl.dll找不到指定的模块

PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl.dll找不到指定的模块

最近在编写windows php多线程的东西,从官网下载了PHP的线程安全版,尝试开启curl扩展extension=php_curl.dllphp -m 却提示 PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl...