PHP:使用 PNG 和 trans 进行裁剪
当用户上传并尝试裁剪 PNG 或透明图像(带有 .gif)时,
我遇到问题:
Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in statusUpFunctions.php on line 100
Warning: imagecreatefromjpeg(): 'images/status/photo/1-6.jpg' is not a valid JPEG file in statusUpFunctions.php on line 100
Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in statusUpFunctions.php on line 114
这可能是因为我的 php 裁剪功能:
case 'crop':
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
ini_set('memory_limit', '256M');
$jpeg_quality = 90;
$src = "images/status/photo/".$_POST['fname'];
$img_r = imagecreatefromjpeg($src);
if($_POST['fixed'] == 0) {
$targ_w = $_POST['w'];
$targ_h = $_POST['h'];
}
else {
$targ_h = $_POST['sizeh'];
$targ_w = $_POST['sizew'];
}
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
$path_thumbs = "images/status/photo";
$filename = $newfilename;
$thumb_path = $path_thumbs . '/' . $filename;
imagejpeg($dst_r,$thumb_path,$jpeg_quality);
}
break;
}
它从 jpg 创建,我不能这样做,所以它从 gif 和 png 创建和 bmp 等等..?然后以某种方式转换它..
这个应该如何解决?
Im having issue when a user uploads and try's to crop a PNG or transparent image( with .gif)
I am getting:
Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in statusUpFunctions.php on line 100
Warning: imagecreatefromjpeg(): 'images/status/photo/1-6.jpg' is not a valid JPEG file in statusUpFunctions.php on line 100
Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in statusUpFunctions.php on line 114
This may be because of my php crop function:
case 'crop':
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
ini_set('memory_limit', '256M');
$jpeg_quality = 90;
$src = "images/status/photo/".$_POST['fname'];
$img_r = imagecreatefromjpeg($src);
if($_POST['fixed'] == 0) {
$targ_w = $_POST['w'];
$targ_h = $_POST['h'];
}
else {
$targ_h = $_POST['sizeh'];
$targ_w = $_POST['sizew'];
}
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
$path_thumbs = "images/status/photo";
$filename = $newfilename;
$thumb_path = $path_thumbs . '/' . $filename;
imagejpeg($dst_r,$thumb_path,$jpeg_quality);
}
break;
}
It creates from jpg, cant i make so it creates from gif and png and bmp and so..? and then convert it somehow..
How should this be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
函数
imagecreatefromjpeg()
从 jpeg 图像创建通用 PHP 图像对象。然后您可以对该对象执行任何您想要的操作。imagecreatefromjpeg()
只会从 JPEG 创建通用图像对象。如果要从 PNG 或 GIF 创建通用图像对象,则需要使用它们各自的函数:imagecreatefrompng()
和imagecreatefromgif()
。检测图像类型的一种快速方法是读取其文件扩展名。一旦有了通用图像对象,您就可以用它执行您想要的操作(包括使用 imagecopyresampled() ),然后使用 imagejpeg() 从它创建 jpeg 图像>。
编辑:
您可以使用
pathinfo()
检测文件的扩展名:如果您从
$_POST
值获取文件名,则需要确保将临时图像文件复制到该文件名。我没有看到您的代码中使用了任何$_FILES
值(也许您正在另一个脚本中执行此操作?)。如果没有,这里有一个关于处理文件上传的好教程。它还涵盖文件扩展名。The function
imagecreatefromjpeg()
creates a generic PHP image object from a jpeg image. You can then do whatever you want with the object.imagecreatefromjpeg()
will ONLY create a generic image object FROM a JPEG. If you want to create a generic image object FROM a PNG or GIF, you need to use their respective functions:imagecreatefrompng()
andimagecreatefromgif()
. One quick way to detect an image's type is by reading its file extension.Once you have that generic image object, then you can do what you want with it (including using
imagecopyresampled()
), and then create a jpeg image from it usingimagejpeg()
.EDIT:
You can use
pathinfo()
to detect the file's extension:If you are getting the filename from a
$_POST
value, you need to make sure that you are copying the temporary image file to that filename. I don't see any$_FILES
values used in your code (maybe you're doing it in another script?). If not, here's a good tutorial on handling file uploads. It also covers file extensions.试试这个...它会起作用..
Try This... It will work..
它无法加载图像,导致 imagecreatefromjpeg() 返回 false。确保您尝试打开的图像有效。
It fails to load the image, causing imagecreatefromjpeg() to return false. Make sure that you're trying to open an valid image.
对于 gif 和 png 文件,您需要使用 imagecreatefromgif 和 imagecreatefrompng 获取图像标识符
For gif and png files you need to get the image identifier using imagecreatefromgif and imagecreatefrompng
这将返回裁剪后的图像。
This will return cropped image.