ElijahLoop Posted February 27, 2021 Share Posted February 27, 2021 There is a picture with dimensions of 10000 by 10000 pixels. Need to display a picture with dimensions of 200 by 100. Only need to use native php. Quote Link to comment https://forums.phpfreaks.com/topic/312223-how-to-crop-the-image-using-native-php/ Share on other sites More sharing options...
Strider64 Posted February 27, 2021 Share Posted February 27, 2021 (edited) Well, it's not actually cropping, but resizing function that I wrote and I have been testing it out on my website. So far it's been working like I expect it to. Though I imagine you could modify the script to do cropping? <?php function imageResize($imageSrc, $imageWidth, $imageHeight, $newImageWidth, $newImageHeight): GdImage|bool { $newImageLayer = imagecreatetruecolor($newImageWidth, $newImageHeight); imagecopyresampled($newImageLayer, $imageSrc, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $imageWidth, $imageHeight); return $newImageLayer; } function resize($file_temp, $new_file_name, $thumb = false): bool { $sourceProperties = getimagesize($file_temp); // Get image sizes: /* * Determine if it's a thumbnail or large image then * resize the images accordingly. */ $old_width = $sourceProperties[0]; $old_height = $sourceProperties[1]; $new_height = null; if ($thumb) { $new_width = 300; $newImageHeight = ($new_width * $old_height) / $old_width; $newImageWidth = $new_width; } else { $new_width = 1200; $newImageHeight = ($new_width * $old_height) / $old_width; $newImageWidth = $new_width; } $temp = '../' . $new_file_name; // folder is nested one down from root: $imageType = $sourceProperties[2];// Determine what type of image (png, jpg or gif): /* * Use Switch statement to resize the image and save it to the correct folders */ switch ($imageType) { case IMAGETYPE_PNG: $imageSrc = imagecreatefrompng($file_temp); $tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1], $newImageWidth, $newImageHeight); imagepng($tmp, $temp); break; case IMAGETYPE_JPEG: $imageSrc = imagecreatefromjpeg($file_temp); $tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1], $newImageWidth, $newImageHeight); imagejpeg($tmp, $temp); break; case IMAGETYPE_GIF: $imageSrc = imagecreatefromgif($file_temp); $tmp = imageResize($imageSrc, $sourceProperties[0], $sourceProperties[1], $newImageWidth, $newImageHeight); imagegif($tmp, $temp); break; default: echo "Invalid Image type."; exit; } return true; // Return true to signal that image was resized: } Edited February 27, 2021 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/312223-how-to-crop-the-image-using-native-php/#findComment-1584790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.