PHP adjust image width and height example sharing

小云云
Release: 2023-03-21 06:40:01
Original
2542 people have browsed it

This article mainly shares with you examples of adjusting the width and height of images in PHP. I hope that these codes can help everyone learn to use PHP to adjust the width and height of images.

/** 
 * 改变图片的宽高 
 *  
 * @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;
Copy after login

Related recommendations:

How to adjust the image size on the server side in php

How to use CSS to adjust the image size Example code sharing

Detailed explanation of the image resize function of php to adjust the image size

The above is the detailed content of PHP adjust image width and height example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!