2017-07-26 74 views
2

原始圖像:調整圖像到一定的規模,而不與PHP裁剪

enter image description here

所需的輸出:(我加黑色邊框的理解袋)

enter image description here

我想將圖像調整爲200/200而不裁剪。查看所需的輸出。

我有這樣的代碼

<?php 
// function created by www.thewebhelp.com 

if (!function_exists("create_square_image")) { 

    function create_square_image($original_file, $destination_file = NULL, $square_size = 96) { 

     if (isset($destination_file) and $destination_file != NULL) { 
      if (!is_writable($destination_file)) { 
       echo '<p style="color:#FF0000">Oops, the destination path is not writable. Make that file or its parent folder wirtable.</p>'; 
      } 
     } 

     // get width and height of original image 
     $imagedata = getimagesize($original_file); 
     $original_width = $imagedata[0]; 
     $original_height = $imagedata[1]; 

     if ($original_width > $original_height) { 
      $new_height = $square_size; 
      $new_width = $new_height * ($original_width/$original_height); 
     } 
     if ($original_height > $original_width) { 
      $new_width = $square_size; 
      $new_height = $new_width * ($original_height/$original_width); 
     } 
     if ($original_height == $original_width) { 
      $new_width = $square_size; 
      $new_height = $square_size; 
     } 

     $new_width = round($new_width); 
     $new_height = round($new_height); 

     // load the image 
     if (substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")) { 
      $original_image = imagecreatefromjpeg($original_file); 
     } 
     if (substr_count(strtolower($original_file), ".gif")) { 
      $original_image = imagecreatefromgif($original_file); 
     } 
     if (substr_count(strtolower($original_file), ".png")) { 
      $original_image = imagecreatefrompng($original_file); 
     } 

     $smaller_image = imagecreatetruecolor($new_width, $new_height); 
     $square_image = imagecreatetruecolor($square_size, $square_size); 

     imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height); 

     if ($new_width > $new_height) { 
      $difference = $new_width - $new_height; 
      $half_difference = round($difference/2); 
      imagecopyresampled($square_image, $smaller_image, 0 - $half_difference + 1, 0, 0, 0, $square_size + $difference, $square_size, $new_width, $new_height); 
     } 
     if ($new_height > $new_width) { 
      $difference = $new_height - $new_width; 
      $half_difference = round($difference/2); 
      imagecopyresampled($square_image, $smaller_image, 0, 0 - $half_difference + 1, 0, 0, $square_size, $square_size + $difference, $new_width, $new_height); 
     } 
     if ($new_height == $new_width) { 
      imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height); 
     } 


     // if no destination file was given then display a png  
     if (!$destination_file) { 
      imagepng($square_image, NULL, 9); 
     } 

     // save the smaller image FILE if destination file given 
     if (substr_count(strtolower($destination_file), ".jpg")) { 
      imagejpeg($square_image, $destination_file, 100); 
     } 
     if (substr_count(strtolower($destination_file), ".gif")) { 
      imagegif($square_image, $destination_file); 
     } 
     if (substr_count(strtolower($destination_file), ".png")) { 
      imagepng($square_image, $destination_file, 9); 
     } 

     imagedestroy($original_image); 
     imagedestroy($smaller_image); 
     imagedestroy($square_image); 
    } 

} 

create_square_image("image.jpg", "sample_thumb.jpg", 200); 
?> 

<h2>Original image</h2> 
<h2><img src="image.jpg" /> 
</h2> 

<h2>The created square thumbnail</h2> 
<h2><img src="sample_thumb.jpg" /> 
</h2> 

但我的代碼輸出這樣

enter image description here

+0

你有一個鏈接到輸出圖像的CSS「class」或「id」嗎? – Ivan

回答

2

有關使用符合您尺寸的白色圖像如何?即,您會得到一個您需要的尺寸(200x200)的白色圖像,並將其與您的其他圖像合併。

// First image is the white image that has the dimension you want. 
$image1 = imagecreatefrompng('COLOR.png'); 

// Second image is the image you want to change size of 
$image2 = imagecreatefrompng('SOURCE.png'); 

// Merge the two, so that the white image is below the "Dress" 
// image you showed us in your question. 
imagecopymerge($image1, $image2, 0, 0, 0, 0, 161, 200, 100); 

// Output and free from memory 
header('Content-Type: image/png'); 
echo imagepng($image1); 

// Destroy images 
imagedestroy($image1); 
imagedestroy($image2); 

參數功能:

布爾imagecopymerge(資源$ dst_im和資源$ src_im分別,詮釋$ dst_x ,詮釋$ dst_y,詮釋$從src_x,詮釋$ src_y,詮釋$ src_w,詮釋$ src_h,詮釋 $ PCT)

最後一個參數是百分比(阿爾法),所以如果你不想要一個白色的底色,那麼你只需將其設置爲0,這將是透明。

編輯:查看文檔,以便獲得第二張圖片here的正確位置。

2編輯:上面的代碼將產生以下圖像(提供的彩色圖像是黑色):

enter image description here

+0

你能寫更多代碼來更好地解釋你的答案嗎? – Umair

+0

什麼似乎不清楚?函數本身還是使用的例子? *編輯:*我更新了我的答案,希望現在有點更清楚。 –

+0

嘗試了您的代碼,什麼都沒有發生......兩張圖片都未被觸摸:P – Umair

0

我已發現,對於這兩種情況的圖像是否是工作溶液寬或高。

$whereToPut = "source.jpg"; // This is 200/200 blank white image 
    $size = getimagesize($fn); 
    $ratio = $size[0]/$size[1]; // width/height 

    $dst_y = 0; 
    $dst_x = 0; 

    if ($ratio > 1) { 
     $width = 200; 
     $height = 200/$ratio; 
     $dst_y = (200 - $height)/2; 
    } else { 
     $width = 200 * $ratio; 
     $height = 200; 
     $dst_x = (200 - $width)/2; 
    } 

    $src = imagecreatefromstring(file_get_contents($fn)); 
    $dst = imagecreatetruecolor($width, $height); 
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); 

    $image1 = imagecreatefromjpeg($whereToPut); 

    imagecopymerge($image1, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100); 
    imagejpeg($image1, $finalImage); 

    // $dinalImage is your final created image.