Jump to content

imgrooot

Members
  • Content Count

    313
  • Joined

  • Last visited

  • Days Won

    1

imgrooot last won the day on May 16 2018

imgrooot had the most liked content!

Community Reputation

1 Neutral

About imgrooot

  • Rank
    Advanced Member

Recent Profile Visitors

1,845 profile views
  1. I am back and I have solved the issue. I just had to move up the $exif code before the orientation switch statement. It rotates the jpeg images now. There is a slight problem however. I'm not sure if it's normal or not. But if I upload a png or gif image, I get this error Warning: exif_read_data(phpA107.tmp): File not supported in 1. It uploads the png and gif images fine. 2. It doesn't compress gif and png images. Only jpegs are compressed. 3. Do I need to apply orientation code to the gif and png images in the function or only to jpeg? Having said that here's the new code. function compressImage($source_url, $destination_url, $quality) { //$quality :: 0 - 100 $info = getimagesize($source_url); $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } if($destination_url == NULL || $destination_url == "" ) { $destination_url = $source_url; } else if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagejpeg($image, $destination_url, $quality); //Free up memory imagedestroy($image); } else if($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagegif($image, $destination_url, $quality); } else if($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imageAlphaBlending($image, true); imageSaveAlpha($image, true); /* chang to png quality */ $png_quality = 9 - round(($quality / 100 ) * 9 ); imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression). //Free up memory imagedestroy($image); } else { return FALSE; } return $destination_url; }
  2. Alright after taking some time off, here is the reworked function. Its' combining compression and orientation. It no longer gives me any errors. But it's still not rotating the image when uploaded. Can you spot what's wrong with the code? function compressImage($source_url, $destination_url, $quality) { //$quality :: 0 - 100 $info = getimagesize($source_url); if($destination_url == NULL || $destination_url == "" ) { $destination_url = $source_url; } else if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagejpeg($image, $destination_url, $quality); //Free up memory imagedestroy($image); } else if($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imageAlphaBlending($image, true); imageSaveAlpha($image, true); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } /* chang to png quality */ $png_quality = 9 - round(($quality / 100 ) * 9 ); imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression). //Free up memory imagedestroy($image); } else { return FALSE; } return $destination_url; }
  3. I am trying but I keep getting same errors. Here is the combined function of compression and rotation. function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); } else if ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); } else if ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); } else { $image = null; } $new_image = imagecreatefromstring(file_get_contents($image)); $exif = exif_read_data($image); if(!empty($exif['Orientation'])) { switch($exif['Orientation']) { case 8: $new_image = imagerotate($new_image,90,0); break; case 3: $new_image = imagerotate($new_image,180,0); break; case 6: $new_image = imagerotate($new_image,-90,0); break; } } imagejpeg($new_image, $destination_url, $quality); return $destination_url; }
  4. Here's what I have tried. It's still not working. I get the following errors. Warning: getimagesize() expects parameter 1 to be string, resource given in Warning: rename(XfdpPCBxns.jpg,../members/images/7/projects/8/): The system cannot find the file specified. (code: 2) in // Also if I var dump the $oriented_image variable in the code below, I get the following output. resource(92) of type (gd) // ORIGINAL CODE WITHOUT THE ORIENTATION $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($source_file, $new_image, 50); $new_file_path = $target_dir . $resized_image; $check = getimagesize($source_file); if(rename($new_image, $new_file_path)) { echo 'success'; } // NEW CODE WITH THE ORIENTATION FUNCTION $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $oriented_image = getOrientedImage($source_file); $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($oriented_image, $new_image, 50); $new_file_path = $target_dir . $resized_image; $check = getimagesize($oriented_image); if(rename($new_image, $new_file_path)) { echo 'success'; }
  5. So whether I am uploading an image through my iphone or sending that image to my computer and uploading it from the computer, it has the same effect. If I upload that image, it'll orient the image in landscape mode. Having said that, I found a function that can fix the orient issue. The problem is I don't know the proper way to integrate it into the image upload script. I have tried several different ways but they all give me errors. Can you show me where exactly I should use this function? // IMAGE ORIENTATION function getOrientedImage($imagePath) { $image = imagecreatefromstring(file_get_contents($imagePath)); $exif = exif_read_data($imagePath); if(!empty($exif['Orientation'])) { switch($exif['Orientation']) { case 8: $image = imagerotate($image,90,0); swapHW(); break; case 3: $image = imagerotate($image,180,0); break; case 6: $image = imagerotate($image,-90,0); swapHW(); break; } } return $image; } // IMAGE UPLOAD SCRIPT if(isset($_FILES['fileToUpload']) AND !empty($_FILES['fileToUpload']["name"])) { if(is_uploaded_file($_FILES['fileToUpload']["tmp_name"])) { $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($source_file, $new_image, 50); $new_file_path = $target_dir . $resized_image; if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } $uploadOk = 1; // Check if image file is a actual image or fake image $check = getimagesize($source_file); if($check !== false) { // echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $errors[] = 'File is not an image!'; $uploadOk = 0; } // Check if file already exists if (file_exists($target_file)) { $errors[] = 'Sorry, file already exists!'; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { $errors[] = 'Sorry, your file size is bigger than 5mb!'; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") { $errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed!'; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if($uploadOk == 0) { $errors[] = 'Sorry, your file was not uploaded!'; // if everything is ok, try to upload file } else { if(rename($new_image, $new_file_path)) { echo 'success'; } else { $errors[] = 'Sorry, there was an error uploading your file!'; } } } else { $errors[] = 'You must upload an image!'; } }
  6. Alright so someone else was able to point out what I was doing wrong. The issue with was this line. I was uploading the original file, which I assumed was needed as the source. if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_file_path)) { The fix is this to change it to this. if(rename($new_image, $new_file_path)) { Here is my full code. It works now. if(isset($_FILES['fileToUpload']) AND !empty($_FILES['fileToUpload']["name"])) { if(is_uploaded_file($_FILES['fileToUpload']["tmp_name"])) { $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compress_image($source_file, $new_image, 10); $new_file_path = $target_dir . $resized_image; if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } $uploadOk = 1; // Check if image file is a actual image or fake image $check = getimagesize($source_file); if($check !== false) { // echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $errors[] = 'File is not an image!'; $uploadOk = 0; } // Check if file already exists if (file_exists($target_file)) { $errors[] = 'Sorry, file already exists!'; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { $errors[] = 'Sorry, your file size is bigger than 5mb!'; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") { $errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed!'; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if($uploadOk == 0) { $errors[] = 'Sorry, your file was not uploaded!'; // if everything is ok, try to upload file } else { if(rename($new_image, $new_file_path)) { echo 'success'; } else { $errors[] = 'Sorry, there was an error uploading your file!'; } } } else { $errors[] = 'You must upload an image!'; } }
  7. I expect the images to go to this directory. $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; And they do go to this directory. I see the images being uploaded with the correct name and extension. The only thing not happening is that those images are not being reduced in file size. They are the exact same size as the original image. Just to clarify, by compressing an image, I meant reducing the image file size. So if the original image file size is 1.6mb, then I want to reduce/compress that to a lower file size and upload it to the directory. I am assuming that's what that function does, unless I am wrong.
  8. I found another compress function that's a little more sophisticated. It uploads the file successfully. But once again it doesn't compress the image. I'm thinking perhaps something is wrong with my original setup? I really need to know exactly what it is instead of hints. I've been at it for over a day and still no luck. function compressImage($source_url, $destination_url, $quality) { //$quality :: 0 - 100 $info = getimagesize($source_url); if($destination_url == NULL || $destination_url == "" ) { $destination_url = $source_url; $info = getimagesize($source_url); } else if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagejpeg($image, $destination_url, $quality); //Free up memory imagedestroy($image); } else if($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imageAlphaBlending($image, true); imageSaveAlpha($image, true); /* chang to png quality */ $png_quality = 9 - round(($quality / 100 ) * 9 ); imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression). //Free up memory imagedestroy($image); } else { return FALSE; } return $destination_url; }
  9. Here is the updated function. I moved the imagejpeg line outside the elseif statement. Still the same issue. Image not compressing. function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); } else if ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); } else if ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); } else {} imagejpeg($image, $destination_url, $quality); return $destination_url; }
  10. 1. For now I am only testing jpeg images. The image I'm testing with is 1.64mb in size. 2. Not sure I understand this question. The compression function I am using is based on these two tutorials. Maybe it'll give you an insight on what I'm trying to do. https://makitweb.com/how-to-compress-image-size-while-uploading-with-php/ https://www.johnpatel.com/compress-images-in-php/
  11. Normally I would agree with you but this problem is little more complex than my brain can handle. I had a good night's rest and still I don't see the solution. Let me walk you through to my thoughts. This is a bit different method than my original code. // RETRIEVING THE DIRECTORY WHERE THE IMAGES ARE LOCATED $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // RETRIEVING THE EXTENSION OF THE IMAGE. E.G. JPG $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // THE SOURCE IMAGE I AM UPLOAD. E.G. php4AC9.tmp $source_image = $_FILES["fileToUpload"]["tmp_name"]; // INSTEAD OF USING THE ORIGINAL NAME, I AM GENERATING A NEW NAME FOR EACH IMAGE I UPLOAD. e.g. Dbo9PxcZi6 $random_name = generateRandomString(10); // THIS WOULD GENERATE A NEW NAME FOR THE IMAGE. E.G. Dbo9PxcZi6.jpg $destination_image = $random_name . '.' . $imageFileType; // THIS COMPRESSES THE IMAGE USING THE SOURCE IMAGE AND THE DESTINATION IMAGE. $resized_image = compress_image($source_image, $destination_image, 50); // NEW FILE PATH FOR THE IMAGE UPLOAD. E.G. ../members/images/7/projects/8/Dbo9PxcZi6.jpg $new_file_path = $target_dir . $resized_image; // CHECKS IF THE IMAGE HAS BEEN UPLOADED TO THE DIRECTORY if(move_uploaded_file($source_image, $new_file_path)) { // SUCCESS } So now that you know my thought process, could you please point out exactly what I am doing wrong? I don't want to keep beating around the bush.
  12. I have tried many different variations of the code. Some work and some don't. But none of the solutions gets me a compressed image. And I doubt I am going to figure out this particular solution by stepping away from the computer. I have already spent hours on it. Posting on this forum is my last resort.
  13. I have an image upload script where I am trying to compress an image to a lower file size. The code works. The image uploads to the directory with the correct name as intended. The only thing is that it's not compression the image as it should be. The size of the new image is the same as the old image. Here's my code. Can you spot what i'm doing wrong? // FUNCTION FOR COMPRESSION function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); } else if ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); } else if ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imagejpeg($image, $destination_url, $quality); } return $destination_url; } // FORM SUBMIT CODE $errors = array(); $db->beginTransaction(); if(isset($_FILES['fileToUpload']) AND !empty($_FILES['fileToUpload']["name"])) { if(is_uploaded_file($_FILES['fileToUpload']["tmp_name"])) { // GENERATES A 10 CHARACTER STRING TO BE USED FOR AN IMAGE NAME $random_name = generateRandomString(10); $global_user_id = 10; $url_project_id = 5; $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } // RESIZE IMAGE $pathname = $_FILES["fileToUpload"]["tmp_name"]; $resized_image = compress_image($pathname, $random_name, 40); $new_file_path = $target_dir . $resized_image . '.' . $imageFileType; // Check if image file is a actual image or fake image $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { // echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $errors[] = 'File is not an image!'; $uploadOk = 0; } // Check if file already exists if (file_exists($new_file_path)) { $errors[] = 'Sorry, file already exists!'; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { $errors[] = 'Sorry, your file size is bigger than 5mb!'; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") { $errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed!'; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if($uploadOk == 0) { $errors[] = 'Sorry, your file was not uploaded!'; // if everything is ok, try to upload file } else { if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_file_path)) { // SUCCESS MESSAGE } else { $errors[] = 'Sorry, there was an error uploading your file!'; } } } else { $errors[] = 'You must upload an image!'; } } if(empty($errors)) { $db->commit(); header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); exit; } else { $db->rollBack(); }
  14. Ah yes that was it. I should've seen that. One more thing. If I want to use this method in my search query, how would you add this code to the above method? WHERE MATCH(title) AGAINST('$search_query' IN BOOLEAN MODE)
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.