我有一个iOS应用程序,可以通过base64将照片上传到我的服务器。
我将图像正常上传到临时位置然后使用PHP我想将其裁剪为方形并将其移动到另一个位置。
照片完美裁剪并且也正确上传到服务器,但新照片(正方形)是黑色的,我找不到解决方案原因。
PHP代码:
// Check if file was upload successfully
if (file_exists($temporaryPath)) {
// Rsize and reduce photo size
list($width, $height) = getimagesize($temporaryPath);
$myImage = imagecreatefromjpeg($temporaryPath);
if ($width > $height) {
$y = 0;
$x = ($width - $height)/2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width)/2;
$smallestSide = $width;
}
$thumbSize = 1080;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
imagejpeg($thumb, $imagePath, 100);
// Check if new image exists
if (file_exists($imagePath)) {
// Upload success
$response = array(
"id"=>"NULL",
"msg"=>"success"
);
echo json_encode(array("user_data"=>$response));
exit();
} else {
// Failed to resize photo
$response = array(
"id"=>"NULL",
"msg"=>"error"
);
echo json_encode(array("user_data"=>$response));
exit();
}
}else{
// Upload failed
$response = array(
"id"=>"NULL",
"msg"=>"upload_failed"
);
echo json_encode(array("user_data"=>$response));
exit();
}
变量 $ temporaryPath 是当前上传的临时照片的路径。
我该怎么办?
谢谢!
答案 0 :(得分:0)
感谢@jcaron我解决了这个问题。我将图像保存为来自iOS的PNG,然后将imagecreatefromjpeg替换为imagecreatefrompng。