php如何实现调整图片大小或创建缩略图-PHP问题

资源魔 57 0

应用如下代码修正图片巨细或创立缩略图。

参数阐明:

$filename:文件名。

$tmpname:文件门路,如上传中的暂时目次。

$xmax:修正后最年夜宽度。

$ymax:修正后最年夜高度。

收费学习视频教程保举:php视频教程

示例代码:

<?php
// 重置图片文件巨细
function resize_image($filename, $tmpname, $xmax, $ymax){    
    $ext = explode(".", $filename);    
    $ext = $ext[count($ext)-1];
     if($ext == "jpg" || $ext == "jpeg")        
     $im = imagecreatefromjpeg($tmpname);    
     elseif($ext == "png")        
     $im = imagecreatefrompng($tmpname);    
     elseif($ext == "gif")        
     $im = imagecreatefromgif($tmpname);
     $x = imagesx($im);    
     $y = imagesy($im);
     if($x <= $xmax && $y <= $ymax)        
     return $im;
     if($x >= $y) {        
     $newx = $xmax;        
     $newy = $newx * $y / $x;    
     }    
     else {        
     $newy = $ymax;        
     $newx = $x / $y * $newy;    
     }
     $im2 = imagecreatetruecolor($newx, $newy);    
     imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);    
     return $im2; 
     }
     ?>

相干文章教程保举:php教程

以上就是php若何完成调整图片巨细或创立缩略图的具体内容,更多请存眷资源魔其它相干文章!

标签: php 实现 方法 php教程 创建 php故障解决 php使用问题 调整 图片大小 缩略图

抱歉,评论功能暂时关闭!