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['mime'], 'jpeg') !== false) { 

        $pic_obj = imagecreatefromjpeg($img_src); 

    } else if (strpos($img_info['mime'], 'gif') !== false) { 

        $pic_obj = imagecreatefromgif($img_src); 

    } else if (strpos($img_info['mime'], 'png') !== 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('~.([^.]+)$~', $new_img_path, $match)) { 

        $new_type = strtolower($match[1]); 

        switch ($new_type) { 

            case 'jpg': 

                imagejpeg($new_img, $new_img_path); 

                break; 

            case 'gif': 

                imagegif($new_img, $new_img_path); 

                break; 

            case 'png': 

                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('http://static.php.net/www.php.net/images/php_snow_2008.gif', 'test.png', '300', '400'); 

var_dump($ret);  die;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值