首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Imagick照片多幅图像大小调整而不失真/stretching php

Imagick照片多幅图像大小调整而不失真/stretching php
EN

Stack Overflow用户
提问于 2016-05-15 16:25:16
回答 1查看 660关注 0票数 1

我希望保持图像的高宽比,因为我调整了它们的大小。我得到了94,000张图片,我需要作为预览图片显示在一个社交网站上。我所面临的挑战是,一些用户上传了完整的照片,结果他们在调整尺寸后显得很紧张。我正在使用代码点火器来实现这一点。文件名位于数据库表中。这是我正在使用的代码

代码语言:javascript
复制
if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles    /purchased_profiles/".$images_->_file_name)) {
    //echo "The file $filename exists";

    $thumb = new Imagick();

    $thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name); 
    $orientation = $thumb->getImageOrientation(); 

    switch($orientation) { 
        case imagick::ORIENTATION_BOTTOMRIGHT: 
            $thumb->rotateimage("#000", 180); // rotate 180 degrees 
        break; 

        case imagick::ORIENTATION_RIGHTTOP: 
            $thumb->rotateimage("#000", 90); // rotate 90 degrees CW 
        break; 

        case imagick::ORIENTATION_LEFTBOTTOM: 
            $thumb->rotateimage("#000", -90); // rotate 90 degrees CCW 
        break; 
    }
    $thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);

    $thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
    $thumb->clear();
    $thumb->destroy(); 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-15 16:43:50

如果图片是以不同的大小上传的,如果我要将这里找到的解决方案how do i use imagick in php? (resize & crop)与您的代码结合起来,这将是一个真正的挑战,我可能会想出以下内容

代码语言:javascript
复制
      if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name)) {



        $thumb = new Imagick();

       $thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name); 

$orientation = $thumb->getImageOrientation(); 

switch($orientation) { 
    case imagick::ORIENTATION_BOTTOMRIGHT: 
        $thumb->rotateimage("#000", 180); // rotate 180 degrees 
    break; 

    case imagick::ORIENTATION_RIGHTTOP: 
        $thumb->rotateimage("#000", 90); // rotate 90 degrees CW 
    break; 

    case imagick::ORIENTATION_LEFTBOTTOM: 
        $thumb->rotateimage("#000", -90); // rotate 90 degrees CCW 
    break; 
}

          //now check the width
        $width=$thumb->getImageWidth();

        //now check height
        $height=$thumb->getImageHeight();

       if ($height>$width) {

         $new_height=160;
         $new_width=(int)($width/$height*160);

         $thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);

         $cropWidth = $thumb->getImageWidth();
         $cropHeight = $thumb->getImageHeight();
      $cropZoom=1;

     if ($cropZoom) {
       $newWidth = $cropWidth / 2;
       $newHeight = $cropHeight / 2;

    $thumb->cropimage(
        $new_width,
        $new_width,
        0,
        0
    );


      }
   } 
     elseif ($width>$height) {
     # code...

        $new_width=160;

        $new_height=(int)($height/$width*160);

       $thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);
         }
       else{

        $thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);
        }   



           $thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
        $thumb->clear();
       $thumb->destroy(); }

你可能需要裁剪,如果图像的高度大于宽度,所以我决定裁剪的尺寸等于宽度从左到角,很可能你不会错过人的脸这种方式。祝好运

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37240747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档