$Obj_old_image
根据您发布的代码,我不知道它在做什么或在做什么。然而,以下代码将适用于 PHP 和安装的标准 GD 库:
// image to be scaled
$rawImgPath = './test.jpg';
// new image size (guessing you know the new size)
$newImgSize['w'] = 200;
$newImgSize['h'] = 200;
// the steps to create the new scaled image
$rawImg = imagecreatefromjpeg($rawImgPath);
$newImg = imagecreatetruecolor($newImgSize['w'], $newImgSize['h']);
// need to know the current width and height of the source image
list($rawImgSize['w'], $rawImgSize['h']) = getimagesize($rawImgPath);
// resize the iamge
imagecopyresampled($newImg,$rawImg, 0,0,0,0,
$newImgSize['w'],$newImgSize['h'],$rawImgSize['w'],$rawImgSize['h']);
// no longer need the original
imagedestroy($rawImg);
// display scaled image
header('Content-Type: image/png');
imagepng($newImg);
// no longer need the scaled image
imagedestroy($newImg);
这段代码应该可以工作并给出更好的结果,但不适合我。基本上,imagescale()
是新代码,并且在 PHP 站点上不是很好的文档。
// image to be scaled
$rawImgPath = './test.jpg';
// new image size (guessing you know the new size)
$newImgSize['w'] = 200;
$newImgSize['h'] = 200;
// the steps to create the new scaled image
$rawImg = imagecreatefromjpeg($rawImgPath);
$newImg = imagescale($rawImg, $newImgSize['w'], $newImgSize['h'],
IMG_BICUBIC_FIXED);
// no longer need the original
imagedestroy($rawImg);
// display scaled image
header('Content-Type: image/png');
imagepng($newImg);
// no longer need the scaled image
imagedestroy($newImg);