【发布时间】:2018-11-07 17:16:34
【问题描述】:
我想在上传后借助 imagecreatefromjpeg 函数调整图像文件的大小,但该函数无法从文件夹中访问文件,因为它会抛出错误,即**
imagecreatefromjpeg(): gd-jpeg: JPEG 库报告不可恢复 错误:在 D:\xampp\htdocs\resize\index.php
** 但是文件已上传,我编写了以下代码。
<form method="post" enctype="multipart/form-data">
<input type="file" name="f1">
<input type="submit" name="btn" value="Upload">
</form>
<?php
ini_set("memory_limit","256M");
if(isset($_POST['btn']))
{
if(move_uploaded_file($_FILES['f1']['tmp_name'], "images/".$_FILES['f1']['name']))
{
$filename = "images/".$_FILES['f1']['name'];
$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
echo "<img src =$filename>";
if( ini_get('allow_url_fopen') ) {
// it's enabled, so do something
$original_img = imagecreatefromjpeg($filename);
$thumb_w = 100;
$thumb_h = 60;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
$thumb_filename = "new.jpg";
imagecopyresampled($thumb_img, $original_img,
0, 0,
0, 0,
$thumb_w, $thumb_h,
$original_w, $original_h);
imagejpeg($thumb_img, $thumb_filename);
imagedestroy($thumb_img);
imagedestroy($original_img);
}
}
}
?>
【问题讨论】:
-
我相信其他人会回答你的具体问题,但我发现(做同样的事情)现在大多数服务器都包含 ImageMagick 库,这使得它变得更加容易。值得一看。
-
我见过,但对我没用
-
您确定要上传
jpeg图像吗?正如函数名称所说imagecreatefromjpeg,我猜它只接受jpeg。 -
是的......
标签: php