从客户那里收到图像后,我想调整它的大小然后存储它。我正在使用这个功能:
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0,
$w, $h, $oldw, $oldh);
return $temp;
}
$image = PIPHP_ImageResize($_FILES['file']['tmp_name'],10,10);
move_uploaded_file($image, $newname);
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到了这些警告:
我该如何解决这个问题!!
imagesx期望图像资源作为第一个参数。您必须使用适当的函数(例如imagecreatefromjpeg或imagecreatefrompng)创建一个。
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
return $temp;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
$uploadedImage = imagecreatefromjpeg($newname);
if (!$uploadedImage) {
throw new Exception('The uploaded file is corrupted (or wrong format)');
} else {
$resizedImage = PIPHP_ImageResize($uploadedImage,10,10);
// save your image on disk
if (!imagejpeg ($resizedImage, "new/filename/path")) {
throw new Exception('failed to save resized image');
}
}
} else {
throw new Exception('failed Upload');
}
Run Code Online (Sandbox Code Playgroud)
我添加了最少且不足的错误处理,您应该检查例如上传文件的格式并使用适当的图像创建器功能,或者检查上传的文件是否是图像。
附录
您可以使用getimagesize函数确定假定上传的图像的类型。
getimagesize 返回一个数组。如果支持图像类型,则 array[2] 值返回图像的类型。如果文件不是图像,或其格式不受支持,则 array[0] 值为零。一旦您知道图像的文件格式,您就可以使用 imagecreatefromxxx 函数之一来创建适当的图像。
| 归档时间: |
|
| 查看次数: |
10897 次 |
| 最近记录: |