如果我试图从现有图像创建新图像。jpeg图像将显示黑色(空白)图像。
我的代码:
$new_path = 'post/'.time().'-myimage.jpeg';
$src="post/myimage.jpeg";
$jpeg_quality = 90;
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor($my_post['w'], $my_post['h']);
imagecopyresampled($dst_r,$img_r,0,0,$my_post['x'],$my_post['y'],$my_post['w'],$my_post['h'],$my_post['w'],$my_post['h']);
header('Content-Type: image/jpeg');
imagejpeg($dst_r, $new_path, $jpeg_quality);
如果在imagejpeg
中使用文件名,jpeg不会输出到stdout,而是保存到给定的文件名。您必须使用null
作为文件名。
imagejpeg($dst_r, null, $jpeg_quality);
如果你还想保存图片,你必须调用两次函数,或者用readfile
之类的函数将文件内容重定向到服务器。