PHP的
GD
库还是很强悍的。之前没玩过,自己根据搜的示例随便试了下 >;<
<?php
function resize($newWidth, $targetFile, $originalFile, $xyz) {
$info = getimagesize($originalFile);
$mime = $info['mime'];
$randomTmp = rand(0, 10000);
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw new Exception('Unknown image type.');
}
$img = $image_create_func($originalFile);
list($width, $height) = getimagesize($originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$image_save_func($tmp, "$randomTmp.$new_image_ext");
$img2 = $image_create_func("$randomTmp.$new_image_ext");
$tmp2 = imagecreatetruecolor(500, 238);
imagecopyresampled($tmp2, $img2, 0, 0, $xyz[0], $xyz[1], 500, (238 + $xyz[1]), $newWidth, (238 + $xyz[1]));
if (file_exists("$randomTmp.$new_image_ext")) {
unlink("$randomTmp.$new_image_ext");
}
if (file_exists($targetFile)) {
unlink($targetFile);
}
$image_save_func($tmp2, "$targetFile.$new_image_ext");
}
$originalFile = __DIR__ . "/test.png";
$targetFile = __DIR__ . "/xxx";
resize(500, $targetFile, $originalFile, array(0, 150));
test.png:
xxx.png: