简体   繁体   English

PHP上传大图像并按比例调整大小,如Photoshop

[英]PHP Uploading a Large image and resizing with proportion like Photoshop

A quick brief. 快速简介。 We currently have to make small, medium and large images based on product images we have on our site. 我们目前必须根据我们网站上的产品图片制作小型,中型和大型图片。 Not being a wiz on PHP I thought I would still try to automate this nightmare. 不是PHP的wiz我认为我仍然会尝试自动化这个噩梦。

The image we upload and dimension of the thumb is below 我们上传的图片和拇指的尺寸如下

800 width x 1400 width LARGE
300 width x 525 width  THUMB

The problem with my PHP script is that it just resizes with scaling to proportion. 我的PHP脚本的问题在于它只是根据比例调整大小。 I want to it to be able to scale down like in Photoshop, you just click shift and the image scales. 我希望它能像Photoshop一样缩小,你只需点击移动,图像就可以缩放。 I am trying to use imagecopyresized but with little luck. 我试图使用imagecopyresized但运气不佳。

<?php
// PHP UPLOAD SCRIPT

// VALIDATION OF FORM

if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000)
{
    // VARS
    $target_folder = 'images/'; 
    $upload_image = $target_folder.basename($_FILES['user_image']['name']); 

    // UPLOAD FUNCTION
    if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image)) 
    {

        // VARS FOR FILE NAMES
        $thumbnail = $target_folder."medium_x.jpg";
        $actual = $target_folder."large_x.jpg";

      // THUMBNAIL SIZE
        list($width, $height) = getimagesize($upload_image);
        $newwidth = "300"; 
        $newheight = "525";

        // VARS FOR CALL BACK
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($upload_image);

        // RESIZE WITH PROPORTION LIKE PHOTOSHOP HOLDING SHIFT
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        // MAKE NEW FILES
        imagejpeg($thumb, $thumbnail, 100);

        // FILE RENAMES
        rename($upload_image, $actual);

        {

            // SUCCESS MESSAGES
            ?>

            <p>
                Image Successfully uploaded!<br />
                Actual image: <?=$actual;?><br />
                Thumbnail image: <?=$thumbnail;?><br />

                <h1>Large</h1>
                <img src="<?=$actual;?>"/><br /><br />
                <h1>Medium</h1>
                <img src="<?=$thumbnail;?>"/><br /><br />
            </p>

            <?
        }
        // FAILED MESSAGES
    }
    else
    {
        echo 'Upload failed.';
    }
}
else
{
        // NOT A JPEG
        echo 'Not a JPEG';
}

// END OF SCRIPT
?>

How can I resize properly without having a stretched image? 如何在没有拉伸图像的情况下正确调整大小? Is it possible? 可能吗?

The easiest way to achieve this without buckets of PHP code is with Image Magick. 在没有PHP代码存储桶的情况下实现这一目标的最简单方法是使用Image Magick。 See examples and sample command line here: http://www.imagemagick.org/Usage/resize/#fill 请参阅示例和示例命令行: http//www.imagemagick.org/Usage/resize/#fill

(can call from PHP using exec() or similar) (可以使用exec()或类似方法从PHP调用)

Edit: Implementation as follows: 编辑:实施如下:

if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000)
{
    // VARS
    $target_folder = 'images/'; 
    $upload_image = $target_folder.basename($_FILES['user_image']['name']);

    $thumbnail = $target_folder."medium_x.jpg";
    $actual = $target_folder."large_x.jpg";

    $newwidth = "300"; 
    $newheight = "525";

    if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image)) 
    {
        exec('convert '.$upload_image.' -resize '.$newwidth.'x'.$newheight.'^ '.$thumbnail);
        rename($upload_image, $actual);
    }
}
...
etc

You could optimise the way you handle the upload but I didn't want too totally change the structure of your code as I don't know what surrounds it. 您可以优化处理上传的方式,但我不想完全改变代码的结构,因为我不知道它周围的内容。 But basically, you push the image to ImageMagick's convert , and it will work it's magic. 但基本上,你将图像推送到ImageMagick的convert ,这将是它的神奇之处。

If you want to crop as well as scale, the exec command would be: 如果你想裁剪和缩放,exec命令将是:

exec('convert '.$upload_image.' -resize '.$newwidth.'x'.$newheight.'^ -gravity center -extent '.$newwidth.'x'.$newheight.' '.$thumbnail);

Look at this code snippet. 看看这段代码。 Does what you need. 你需要什么 Basically used to maintain ratio when creating the thumb. 主要用于在创建拇指时保持比例。 http://snipplr.com/view/753/ http://snipplr.com/view/753/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM