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學習者快速成長!