Php 创建完美的缩略图

Php 创建完美的缩略图,php,Php,下面的代码工作正常,没有任何错误,但我的问题是,当我创建一个缩略图时,有时缩略图是不可理解的(某些情况下,例如宽度非常大于高度),我还尝试了一个自动计算高度的代码。但它不能完美地工作。我想要一个代码,创建一个可理解的缩略图每次。(裁剪缩略图可以生成) 您可以使用类SimpleImage,如: // Usage: // Load the original image $image = new SimpleImage('lemon.jpg'); // Resize the image to 600

下面的代码工作正常,没有任何错误,但我的问题是,当我创建一个缩略图时,有时缩略图是不可理解的(某些情况下,例如宽度非常大于高度),我还尝试了一个自动计算高度的代码。但它不能完美地工作。我想要一个代码,创建一个可理解的缩略图每次。(裁剪缩略图可以生成)


您可以使用
类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');

你可以在github上找到这个
,我已经写了一个脚本来制作风景画或肖像画。也许这对你有帮助

<?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');
?>

但是,您可以根据需要更改拇指的来源、拇指宽度、拇指高度和目标。

可以动态调整图像大小。它将在服务器上缓存图像,并使其可在浏览器和代理服务器上缓存。大小调整发生在加载图像时,而不是加载HTML时,因此HTML的加载速度更快。

代码函数make_thumb($src,$dest,$dest,$desired_width){$source_image=imagecreatefromjpeg($src);$width=imagesx($source_image);//即使高度是使用$desired_height=floor自动计算的($height*($desired_width/$width));$virtual_image=ImageCreateTureColor($desired_width,$desired_height);imagecopyresampled($virtual_image,$source_image,0,0,0,$dest_width,$dest);}
<?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');
?>