2013-03-02 74 views
0

下面的代碼工作沒有任何錯誤,但我的問題是當我創建縮略圖有時縮略圖是不可理解的(一些條件如寬度比高度大)我也試過一個代碼計算高度自動。但它不會完美的作品。我想這將創建一個可以理解的縮略圖每次一碼(可生成裁剪縮略圖)創建完美的縮略圖

function make_thumb($src, $dest, $desired_width) 
{ 
    $source_image = imagecreatefromjpeg($src); 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    //even if height is calculated automatically using 
    $desired_height = floor($height * ($desired_width/$width)); 

    $virtual_image = imagecreatetruecolor($desired_width, $desired_height); 
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); 
    imagejpeg($virtual_image, $dest); 
} 
+0

代碼

 function make_thumb($src, $dest, $desired_width) { $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); //even if height is calculated automatically using $desired_height = floor($height * ($desired_width/$width)); $virtual_image = imagecreatetruecolor($desired_width, $desired_height); imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); imagejpeg($virtual_image, $dest); }
2013-03-02 15:54:11

回答

1

可以使用Class SimpleImage,如:

// Usage: 
// Load the original image 
$image = new SimpleImage('lemon.jpg'); 

// Resize the image to 600px width and the proportional height 
$image->resizeToWidth(600); 
$image->save('lemon_resized.jpg'); 

你可以找到這個class這裏githubhttps://gist.github.com/miguelxt/908143

1

我寫了一個腳本來製作風景或肖像圖像的大拇指。可能這會幫助你

<?php 
    $thumbWidth = 200; // can change it to whatever required 
    $thumbHeight = 200; // can change it to whatever required 

    $img = imagecreatefromstring(file_get_contents('SAM_1883.JPG')); 
    $imgWidth = imagesx($img); 
    $imgHeight = imagesy($img); 

    $imgStart_x = 0; 
    $imgStart_y = 0; 
    $imgEnd_x = $imgWidth; 
    $imgEnd_y = $imgHeight; 

    if($imgWidth > $imgHeight){ 
     $diff = $imgWidth - $imgHeight; 
     $imgStart_x = $diff/2; 
     $imgEnd_x = $imgWidth - $diff; 
    }else{ 
     $diff = $imgHeight - $imgWidth; 
     $imgEnd_y = $imgHeight - $diff; 
    } 

    $dest = imagecreatetruecolor($thumbHeight,$thumbHeight); 
    imagecopyresized($dest, $img, 0, 0, $imgStart_x, $imgStart_y, $thumbWidth, $thumbHeight, $imgEnd_x, $imgEnd_y); 
    imagePNG($dest,'abc'.rand(0,9999).'.png'); 
?> 

但是,您可以根據您的要求更改來源,thumbWidth,thumbHeight和拇指目的地。

0

https://github.com/lencioni/SLIR可以隨時調整圖像大小。它會將圖像緩存在服務器上,並使其可在瀏覽器和代理服務器上緩存。調整大小會在加載圖像時發生,而不是在加載HTML時加快HTML加載速度。