关于php图片等比例缩放函数的.谁能帮我看看我这个函数怎么用啊?

php中文网
发布: 2016-06-06 20:18:31
原创
1616人浏览过

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) 
{ 
   
    list($width, $height, $type, $attr) = getimagesize($srcImage); 
    if($width < $maxWidth  || $height < $maxHeight) return ; 
    switch ($type) { 
    case 1: $img = imagecreatefromgif($srcImage); break; 
    case 2: $img = imagecreatefromjpeg($srcImage); break; 
    case 3: $img = imagecreatefrompng($srcImage); break; 
    } 
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 
     
    if($scale < 1) { 
    $newWidth = floor($scale*$width); 
    $newHeight = floor($scale*$height); 
    $newImg = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    $newName = ""; 
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); 
 
    switch($type) { 
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) 
        return "$newName.gif"; break; 
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) 
        return "$newName.jpg"; break; 
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) 
        return "$newName.png"; break; 
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break; 
    } 
    imagedestroy($newImg); 
    } 
    imagedestroy($img); 
    return false; 
}
其中 $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);具体目的是什么?
登录后复制

回复内容:

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) 
{ 
   
    list($width, $height, $type, $attr) = getimagesize($srcImage); 
    if($width < $maxWidth  || $height < $maxHeight) return ; 
    switch ($type) { 
    case 1: $img = imagecreatefromgif($srcImage); break; 
    case 2: $img = imagecreatefromjpeg($srcImage); break; 
    case 3: $img = imagecreatefrompng($srcImage); break; 
    } 
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 
     
    if($scale < 1) { 
    $newWidth = floor($scale*$width); 
    $newHeight = floor($scale*$height); 
    $newImg = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    $newName = ""; 
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); 
 
    switch($type) { 
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) 
        return "$newName.gif"; break; 
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) 
        return "$newName.jpg"; break; 
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) 
        return "$newName.png"; break; 
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break; 
    } 
    imagedestroy($newImg); 
    } 
    imagedestroy($img); 
    return false; 
}
其中 $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);具体目的是什么?
登录后复制

因为tofile($srcimage,$tofile)第二个参数$tofile自己指定的话存在格式问题,比如srcimage是jpg,但是如果你填写的tofile是png,那格式就对不上了,因此如果你制定了tofile的文件后缀,那就直接干掉,然后在直接将原图片的格式追加在你指定的tofile后面。

--

简单来说:
tofile('a.jpg','b.png');
最终生成的是b.jpg而不是b.png

AI Word
AI Word

一款强大的 AI 智能内容创作平台,致力于帮助用户高效生成高质量、原创且符合 SEO 规范的各类文章。

AI Word 226
查看详情 AI Word
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号