2016-05-15 46 views
1

我希望在調整它們的大小時保持圖像的寬高比。我需要在社交網站上顯示94000個圖像作爲預覽圖像。我得到的挑戰是一些用戶上傳了全長照片,結果它們在重新調整大小後顯得很緊張。我正在使用代碼點火器來實現這一點。文件名在數據庫表中。這是我使用Imagick照片多圖像調整大小作物沒有扭曲/拉伸php

 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(); 
    } 

回答

1

如果圖像大小不同其上傳一個真正的挑戰代碼如果我在這裏找到how do i use imagick in php? (resize & crop)和你的代碼的解決方案結合我可以想出以下

 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(); } 

如果圖像高度大於寬度,您可能需要裁剪,所以我決定裁剪的尺寸等於從左到右的寬度,最有可能不會以這種方式錯過人臉。祝你好運

+0

謝謝你工作像魔術甚至沒有編輯你的代碼 –