imagecopy - 裁剪图像而不是调整大小

时间:2014-12-15 01:53:38

标签: php html gd

我想调整大小 - $artfile,并将商店从$dest开始存放到$start_x, $start_y

$dest包含一个框架。 $artfile需要适合框架,所以我需要调整它。

我的代码是:

imagecopy($dest, $art_file, $start_x, $start_y, 0, 0, $new_width, $new_height); // works fine but to test resize

$dest = my destination resource
$artfile = resource that I want to patch with $destination
$new_width, $new_height = I want $art_file to be resized to this value, without trimming off or cropping. 

我的问题是:

对于任何超过$new_height$new_width维度的图片,剪掉的图片越多。我想将整个集调整为$new_height$new_width

任何帮助?

1 个答案:

答案 0 :(得分:0)

玩这个,希望它可以帮助你一些;

 <?php
 Function createthumbnail($src=null,$newHeight=null) {
    $srcimg = imagecreatefromjpeg($src);
    $srcsize = getimagesize($src);
    $dest_y = $newHeight;
    $dest_x = ($newHeight / $srcsize[1]) * $srcsize[0];
    $thumbimg = imagecreatetruecolor($dest_x, $dest_y);
    imagecopyresampled($thumbimg,$srcimg,0,0,0,0,$dest_x,$dest_y, $srcsize[0], $srcsize[1]);
    $thumbimg = imagejpeg($thumbimg,$src);
    return $thumbimg;
}
?>

你可以这样称呼它

<?php
resizedImage = createthumbnail(urlOfFileToResize,newHeight);//newHeight would be like 1 or 50 or 1000......
?>

现在您调整大小的图像将存储在resizedImage

欢迎你。