2013-03-02 46 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加载速度。