【问题标题】:php image resize - squared / cropped thumbnail from center?php图像调整大小 - 从中​​心平方/裁剪缩略图?
【发布时间】:2012-03-13 16:26:14
【问题描述】:

我有一个图片上传脚本,可以将上传的图片大小调整为 150x150 像素。如果图片是方形的,那就太好了,但如果有人上传了一张 640x200 像素的图片,它看起来就不漂亮了。

所以我基本上需要它来根据图像的中心自动创建一个方形缩略图。如果图像较宽,则应裁剪掉左右两侧。如果图像较高,则应裁剪掉顶部和底部。

我在网上找到了一个代码修改,确切地说是:

Upload, resize, and crop center of image with PHP

我对 PHP 不是很好,我已经做了几个小时了,试图将下面的代码与上面的选项结合起来。如果有人可以帮助我,那就太好了:)

                                $target_path = "avatars/";
        $image     = $_FILES['uploadedfile']['name'];
        $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
        $_POST["userpic"]=$_FILES['uploadedfile']['name'];
        if($_FILES['uploadedfile']['tmp_name']!="") {
        $imagetype=explode(".",$_POST["userpic"]);

        if($imagetype[1]=="jpg" || $imagetype[1]=="JPG" || $imagetype[1]=="gif" || $imagetype[1]=="GIF")
        {
        $target_path = "avatars/";
        $thaid=$_POST["user_id"];
        $target_path = $target_path .$thaid.".".$imagetype[1]; 
        $target_path2 =$thaid.".".$imagetype[1]; 
        move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
        $_POST["userpic"]=$target_path2;
        $n_width=$setts['avatar_width']; 
        $n_height=$setts['avatar_height']; 
        $tsrc=$target_path; 
        $add=$target_path;

        if($imagetype[1]=="jpg" || $imagetype[1]=="JPG")
        {
        $im=imagecreatefromjpeg($add);
        $width=imagesx($im); 
        $height=imagesy($im); 
        $newimage=imagecreatetruecolor($n_width,$n_height);


        $ar = 1.00;

    if ($ar < 1) { // "tall" crop
$cropWidth = min($height * $ar, $width);
$cropHeight = $cropWidth / $ar;
    }
    else { // "wide" crop
$cropHeight = min($width / $ar, $height);
$cropWidth = $cropHeight * $ar;
    }


        imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$cropWidth,$cropHeight);
        imagejpeg($newimage,$tsrc,100);
        }
        if($imagetype[1]=="gif" || $imagetype[1]=="GIF")
        {
        $im=imagecreatefromgif($add);
        $width=imagesx($im);              
        $height=imagesy($im);            
        $newimage=imagecreatetruecolor($n_width,$n_height);
        imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
        imagegif($newimage,$tsrc,100);
        }
        }
        else
        {
        $_POST["userpic"]="noimage.jpg";
        }
        } 

【问题讨论】:

    标签: php image resize thumbnails crop


    【解决方案1】:

    计算要裁剪区域的尺寸的数学并不复杂。我已经给this question 提供了一个答案,它允许您针对裁剪区域的任何纵横比进行计算;因为你想要一个方形缩略图,你应该将纵横比设置为 1。

    然后,知道原始图像和缩略图的尺寸后,很容易计算出需要传递给imagecopyresized 的值。

    【讨论】:

    • 非常感谢!我设法将脚本编辑到这个,除了它是从左侧裁剪图像而不是居中之外,它正在工作。有什么我想念的想法吗?
    • 非常感谢!我设法将脚本编辑到这个,除了它是从左侧裁剪图像而不是居中之外,它正在工作。有什么我想念的想法吗?
    • @user1227914:在一般情况下,裁剪区域内最左边的像素是($imageWidth - $cropWidth) / 2(而不是固定的0)。垂直维度也是如此。
    【解决方案2】:

    如果您安装了 ImageMagick,您可以执行以下操作:

    <?php
    $i = new Imagick();
    $i->readImage($file);
    $i->cropThumbnailImage($w,$h);
    

    这样,您实际上不必担心数学问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2011-11-11
      • 2015-03-05
      • 2011-09-23
      • 2010-11-03
      • 1970-01-01
      相关资源
      最近更新 更多