使用PHP调整图像大小 [英] Resize images with PHP

查看:110
本文介绍了使用PHP调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的问题,我不太确定要设置。我在其他地方见过例子但没有特别喜欢我的情况。我想使用PHP调整图像大小,因此它们是可读的,而不仅仅是像你使用HTML一样令人兴奋。如果它们不是250像素宽,或160像素高,我怎样才能调整图片大小以使其成比例但适合该空间?

I have a quick question that I'm not quite sure to set up. I've seen examples elsewhere but nothing specifically like my situation. I would like to resize images using PHP so they're readable and not just wonkily stretched like if you use HTML. If they're not 250 pixels wide, or 160 pixels tall, how can I resize the picture so it's proportionate but fits within that space?

谢谢!

推荐答案

好的,下面是我在商店中使用的Image对象。它维持规模 - 需要GD

Ok, so below is an Image object that I use in my store. It maintains scale - requires GD

<?php
class Store_Model_Image extends My_Model_Abstract 
{
    const PATH  = STORE_MODEL_IMAGE_PATH;
    const URL   = "/store-assets/product-images/";

    public function get_image_url($width, $height)
    {
        $old_file = self::PATH . $this->get_filename();
        $basename = pathinfo($old_file, PATHINFO_FILENAME);

        $new_name = sprintf("%s_%sx%s.jpg", $basename, $width, $height);
        if(file_exists(self::PATH . $new_name))
        {
            return self::URL . $new_name;
        }
        else
        {
            list($width_orig, $height_orig, $image_type) = @getimagesize($old_file);
            $img = FALSE;

            // Get the image and create a thumbnail     
            switch($image_type)
            {
                case 1:
                    $img = @imagecreatefromgif($old_file);
                    break;
                case 2:
                    $img = @imagecreatefromjpeg($old_file);
                    break;
                case 3: 
                    $img = @imagecreatefrompng($old_file);
                    break;
            }

            if(!$img)
            {
                throw new Zend_Exception("ERROR: Could not create image handle from path.");
            }

            // Build the thumbnail
            if($width_orig > $height_orig)
            {
                $width_ratio = $width / $width_orig;
                $new_width   = $width;
                $new_height  = $height_orig * $width_ratio;
            }
            else
            {
                $height_ratio = $height / $height_orig;
                $new_width    = $width_orig * $height_ratio;
                $new_height   = $height;
            }

            $new_img = @imagecreatetruecolor($new_width, $new_height);

            // Fill the image black
            if(!@imagefilledrectangle($new_img, 0, 0, $new_width, $new_height, 0))
            {           
                throw new Zend_Exception("ERROR: Could not fill new image");
            }

            if(!@imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig))
            {
                throw new Zend_Exception("ERROR: Could not resize old image onto new bg.");
            }

            // Use a output buffering to load the image into a variable
            ob_start();
            imagejpeg($new_img, NULL, 100);
            $image_contents = ob_get_contents();
            ob_end_clean();

            // lastly (for the example) we are writing the string to a file
            $fh = fopen(self::PATH . $new_name, "a+");
            fwrite($fh, $image_contents);
            fclose($fh);

            return self::URL . $new_name;
        }
    }
}

我根据要求调整图片大小时间,因此页面第一次加载图像时将调整大小为模板所需的大小。 (这意味着每次我的设计更改时,我都不必崩溃尝试重新生成图像缩略图的共享主机)

I resize the image at request time, so the first time the page loads an image will be resized to the required size for the template. (this means I don't have to crash a shared host trying to regenerate image thumbnails everytime my design changes)

所以在模板中传递图像对象,以及何时你需要一个图像拇指,

So in the template you pass your image object, and when you need a image thumb,

<img src="<?php echo $image->get_image_url(100, 100); ?>" />

你现在有一个100x100的拇指,它被保存到服务器以便以后重复使用

you now have a 100x100 thumb, which is saved to the Server for reuse at a later date

这篇关于使用PHP调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆