Reputation: 9
I have an image uploaded by a HTML form. After checking for a valid image it is saved to a tmp - folder. Later I want to move the file from the tmp - folder to the final uploads folder. For this i wrote the following function.
// 1. image was uploaded via html form to the folder "images/tmp/" with validation checks for image format
// 2. show image from the "images/tmp/" folder on a summary page next to other form input data to the user
// 3. after clicking "save changes" on the summary page the following function is called
// 3.1 check if image size is big depending on $image_resolution calculated by getimagesize() function
// 3.1.1 if image is bigger than $MAX_IMAGE_RESOLUTION --> resize it down to a width of 1600px and safe to "images/uploads" as .webp file
// 3.1.2 if image is smaller than $MAX_IMAGE_RESOLUTION --> just copy the file from "images/tmp/" to "images/uploads" as .webp file
function move_img_to_upl ($tmp_file, $upl_file, $field_name) {
global $UPPER_DIR; // "../"
global $UPL_DIR_IMG; // "images/uploads/
global $TMP_DIR_IMG; // "images/tmp/"
global $MAX_IMAGE_RESOLUTION; // 1707200
global $MAX_IMAGE_WIDTH; // 1600
global $IMAGE_QUALITY; // 90
$separator = "_";
$tempdir = $UPPER_DIR.$TMP_DIR_IMG;
$uploaddir = $UPPER_DIR.$UPL_DIR_IMG;
$image_size = getimagesize($tempdir.$tmp_file);
$old_width = $image_size[0];
$old_height = $image_size[1];
$image_resolution = $old_width * $old_height;
// if image resolution > 1707200 px --> resize to a max width of 1600 px
if ($image_resolution > $MAX_IMAGE_RESOLUTION) {
$image_ratio = $old_height / $old_width;
$new_height = round($MAX_IMAGE_WIDTH * $image_ratio);
$new_width = $MAX_IMAGE_WIDTH;
$image_name = pathinfo($tempdir.$tmp_file, PATHINFO_FILENAME);
$destination = $uploaddir.$image_name.".webp";
$image_old_mime_type = mime_content_type($tempdir.$tmp_file);
if ($image_old_mime_type === "image/png") {
$image_old = imagecreatefrompng($tempdir.$tmp_file);
}
elseif ($image_old_mime_type === "image/jpeg") {
$image_old = imagecreatefromjpeg($tempdir.$tmp_file);
}
elseif ($image_old_mime_type === "image/jpg") {
$image_old = imagecreatefromjpeg($tempdir.$tmp_file);
}
elseif ($image_old_mime_type === "image/webp") {
$image_old = imagecreatefromwebp($tempdir.$tmp_file);
}
$image_new = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_new, $image_old, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
imagewebp($image_new, $destination, $IMAGE_QUALITY);
unlink($tempdir.$tmp_file);
imagedestroy($image_old);
imagedestroy($image_new);
}
// if image resoultion <= 1707200 px --> just move the file from the tmp directory to the uploads directory
else {
rename($tempdir.$tmp_file, $uploaddir.$upl_file);
}
}
Only when a big .heic file is uploaded the script seems to rotate the image bei -90deg. When a small .heic file is uploaded it keeps like the original one.
Hopefully one of you has a solution for that. Thanks in advance Tom
I also tested playing arround with the $MAX_IMAGE_RESOLUTION and $MAX_IMAGE_WIDTH variables. But it seems to depend only on the image size.
Upvotes: 0
Views: 29