Php corrupted image upload

I have a photo upload area. it was working without any problems. But then I started to notice that it didn't upload some images. in others, colors and pixels began to appear incorrect. No problems with old files. only some of the new uploads. This problem started to occur on its own without any changes in my codes. I'm cropping a photo using cropper js. not every upload is like this. I leave the interesting examples here. sometimes it doesn't load the image at all. How can something that works properly fail by itself? sorry my bad english. thanks in advance to everyone who helped
$file = $_FILES['avatar'];
$fileName = $_FILES['avatar']['name'];
$fileTmpName = $_FILES['avatar']['tmp_name'];
$fileError = $_FILES['avatar']['error'];
$fileSize = $_FILES['avatar']['size'];
$fileType = $_FILES['avatar']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
if($fileSize < 100000000){
$fileNameNew = time().md5(time().$o_id.$fileName)."_".sha1($user_id.rand(10,99)).$l_category.".".$fileActualExt;
$fileDestination = "photos/".$fileNameNew;
function compressImage($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return true;
}
compressImage($fileTmpName, $fileDestination, 90);
}
Answer
Solution:
I would check if these are filetype related or not. First and last one could be paletted PNG with shifted or partially applied/recognised palette. The middle one should be a processing error as it has a visible split of contrast in the upper part.
Should be sort of autoexposure that is running on them and fails to do it's job well?
I would say this isn't upload or file error as that'll cause the file to be roken totally 'after' the error and most likely GD won't do a getimagesize() on them nor can use those files.
Also some PHP-related hints:
The pathinfo() is used to get the 'last' extension.
$fileActualExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
That imagecreatefrom* is pain in the ass, but you can use a helper to make it better, also you should use the getimagesize(), imagejpeg() and iamgecreatefrom*() return values for checking if they were really okay.
function getGDImage($source) {
if (!($info = getimagesize($source))) {
return false;
}
switch($info['mime']) {
case 'image/jpeg': return imagecreatefromjpeg($source);
case 'image/gif': return imagecreatefromgif($source);
case 'image/png': return imagecreatefrompng($source);
default: return false;
}
}
function compressImage($source, $destination, $quality) {
return ($image = getGDImage($source))
? imagejpeg($image, $destination, $quality)
: false;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.