php调整图片宽高实例分享

小云云
发布: 2023-03-21 06:40:01
原创
2498 人浏览过

本文主要和大家分享php调整图片宽高实例,希望一下这些代码能帮助大家学会用php调整图片宽高。

/** 
 * 改变图片的宽高 
 *  
 * @author flynetcn (2009-12-16) 
 *  
 * @param string $img_src 原图片的存放地址或url  
 * @param string $new_img_path  新图片的存放地址  
 * @param int $new_width  新图片的宽度  
 * @param int $new_height 新图片的高度 
 * @return bool  成功true, 失败false 
 */  
function resize_image($img_src, $new_img_path, $new_width, $new_height)  
{  
    $img_info = @getimagesize($img_src);  
    if (!$img_info || $new_width < 1 || $new_height < 1 || empty($new_img_path)) {  
        return false;  
    }  
    if (strpos($img_info[&#39;mime&#39;], &#39;jpeg&#39;) !== false) {  
        $pic_obj = imagecreatefromjpeg($img_src);  
    } else if (strpos($img_info[&#39;mime&#39;], &#39;gif&#39;) !== false) {  
        $pic_obj = imagecreatefromgif($img_src);  
    } else if (strpos($img_info[&#39;mime&#39;], &#39;png&#39;) !== false) {  
        $pic_obj = imagecreatefrompng($img_src);  
    } else {  
        return false;  
    }  
    $pic_width = imagesx($pic_obj);  
    $pic_height = imagesy($pic_obj);  
    if (function_exists("imagecopyresampled")) {  
        $new_img = imagecreatetruecolor($new_width,$new_height);  
        imagecopyresampled($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);  
    } else {  
        $new_img = imagecreate($new_width, $new_height);  
        imagecopyresized($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);  
    }  
    if (preg_match(&#39;~.([^.]+)$~&#39;, $new_img_path, $match)) {  
        $new_type = strtolower($match[1]);  
        switch ($new_type) {  
            case &#39;jpg&#39;:  
                imagejpeg($new_img, $new_img_path);  
                break;  
            case &#39;gif&#39;:  
                imagegif($new_img, $new_img_path);  
                break;  
            case &#39;png&#39;:  
                imagepng($new_img, $new_img_path);  
                break;  
            default:  
                imagejpeg($new_img, $new_img_path);  
        }  
    } else {  
        imagejpeg($new_img, $new_img_path);  
    }  
    imagedestroy($pic_obj);  
    imagedestroy($new_img);  
    return true;  
}  
  
//test  
$ret = resize_image(&#39;http://static.php.net/www.php.net/images/php_snow_2008.gif&#39;, &#39;test.png&#39;, &#39;300&#39;, &#39;400&#39;);  
var_dump($ret);  die;
登录后复制

相关推荐:

php如何在服务器端上调整图片大小的方法介绍

如何使用CSS调整图片大小的实例代码分享

php调整图片大小的image resize函数详解

以上是php调整图片宽高实例分享的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!