【问题标题】:Crop image without stretching it, inside PHP [closed]在 PHP 中裁剪图像而不拉伸它[关闭]
【发布时间】:2013-05-15 07:00:10
【问题描述】:

我正在 PHP 中制作上传表单。虽然我希望表单上传一个精确的缩略图,比如说,100px 高和 100px 宽。表单必须在不拉伸图像的情况下重新缩放缩略图,而是从图像中删除一部分。我更喜欢用 100% php 和最简单的脚本来做这件事。我已经制作了上传表单,我只想知道如何用我的缩略图制作这个自动裁剪系统。

我真的希望你能帮助我,我想提前感谢你。

我的脚本已经上传了一张带有缩略图的图片:

if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }

$path= 'images/' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$thumb_path= 'images/thumb_' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$imgsize2= getimagesize( $imgtemp );
$width= $imgsize2[0];
$height= $imgsize2[1];

$maxwidth= 1281;
$maxheight= 721;
$allowed= array( 'image/png', 'image/jpeg', 'image/gif', );

if( in_array( $imgtype, $allowed ) ){

    if( $width < $maxwidth && $height < $maxheight ){

    if( $imgsize < 5242880 ){

        if( $width == $height ){ $case=1; }
        if( $width > $height ){ $case=2; }
        if( $width < $height ){ $case=3; }

        switch( $case ){

            case 1:

            $newwidth= 100;
            $newheight= 100;

            break;

            case 2:

            $newheight= 100;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            case 3:

            $newwidth= 100;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            }

            switch( $imgtype ){

                case 'image/jpeg';

                $img= imagecreatefromjpeg( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagejpeg( $thumb, $thumb_path );  

                break;

                case 'image/png';

                $img= imagecreatefrompng( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagepng( $thumb, $thumb_path ); 

                break;

                case 'image/gif';

                $img= imagecreatefromgif( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagegif( $thumb, $thumb_path ); 

                break;

                } if(empty($errors))  {

        move_uploaded_file( $imgtemp, $path );
        $upimage = "Image is successfully uploaded.";
                }
        } else{
        $errors[9] = "The image you just uploaded does not meet the requirements. Your picture is too large. ";
        }

        } else{
        $errors[10] = "The image you just uploaded does not meet the requirements. It is a forbidden extension.";
        }

    } else{
        $errors[11] = "The image you just uploaded does not meet the requirements. It is a forbidden extension. Type: $imgtype,  $image, $imgsize, $imgtemp, $name";
        }
if(empty($errors))  {
move_uploaded_file( $imgtemp, $path );
}
     }

【问题讨论】:

  • 你真的没有尝试过谷歌搜索吗? google.com/search?q=php+crop+image
  • 我做到了,但它要么太难,要么不是我的意思。但是感谢您的帮助。
  • 哪一部分难?这个网站是用来询问你被困的事情的问题,而不是要求你甚至还没有尝试解决的问题的完整解决方案。
  • sgroves,你说的不是真的。为什么你会觉得我没有做研究?问题是如何在不将图像拉伸到 PHP 上传表单中的情况下裁剪图像。我的脚本已经制作了缩略图,但我希望它上传具有精确宽度和高度的缩略图

标签: php image-processing crop


【解决方案1】:

我相信您需要设置 x 和 y 坐标来裁剪图像。尝试这样的事情。您可能不得不使用top 和left 数字,直到您将它放在您想要的位置。记下imagecopyresized 行。 这里还有一个类似的帖子,答案相同PHP - cropping image with imagecopyresampled()? 您需要设置 x 和 y 参数,如下所述。顺便说一句,答案真的很容易找到。 SO 甚至在我为您找到另一个示例的页面右侧提供了相关问题。干杯。

          switch( $case ){

        case 1:
        $top = 50;
        $left = 50;   
        $newwidth= 100;
        $newheight= 100;

        break;

        case 2:

        $newheight= 100;
        $ratio= $newheight / $height;
        $newwidth= round( $width * $ratio );

        break;

        case 3:

        $newwidth= 100;
        $ratio= $newwidth / $width;
        $newheight= $height * $ratio;

        break;

        }

        switch( $imgtype ){

            case 'image/jpeg';

            $img= imagecreatefromjpeg( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,$left,$top, $newwidth, $newheight, $width, $height );
            imagejpeg( $thumb, $thumb_path );  

            break;

            case 'image/png';

            $img= imagecreatefrompng( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
            imagepng( $thumb, $thumb_path ); 

            break;

            case 'image/gif';

            $img= imagecreatefromgif( $imgtemp );
            $thumb= imagecreatetruecolor( $newwidth, $newheight );
            imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
            imagegif( $thumb, $thumb_path ); 

            break;

【讨论】:

  • 非常感谢!它真的帮助了我。我想知道为什么有些人无法理解我的问题。我真的很感激你的理解。
  • 没问题,很高兴我能提供帮助。它是有效的,您甚至提供了只需要 small 调整的工作代码。仅仅因为似乎没有人理解您的问题并不意味着这不是一个真正的问题。我知道这有时很烦人。无论如何,我在我的网站上上传图片方面做了很多工作,所以我知道你在说什么。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 2016-06-06
  • 2012-10-31
  • 2016-09-11
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多