oz11 Posted July 13, 2022 Share Posted July 13, 2022 (edited) <?php function resize_image($image_name) { echo $image_name = "images/".$image_name; $path_parts = pathinfo('$image_name'); $path_parts['extension']; if ($path_parts['extension'] = "jpg") { // Assign image file to variable //$image_name; // Load image file $image = imagecreatefromjpeg ($image_name); list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; // Use imagescale() function to scale the image $img = imagescale( $image, $new_width, $new_height ); //Output image in the browser header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] = "png"){ //echo "png ;)"; //$image_name = //'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'; // Load image file $image = imagecreatefrompng($image_name); list($width, $height) = getimagesize($image_name); // Use imagescale() function to scale the image $img = imagescale( $image, 300, ($height/$width)*$new_width ); // Output image in the browser header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] = "gif"){ //echo "png ;)"; //$image_name = //'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'; // Load image file $image = imagecreatefromgif($image_name); list($width, $height) = getimagesize($image_name); // Use imagescale() function to scale the image $img = imagescale( $image, 300, ($height/$width)*$new_width ); // Output image in the browser header("Content-type: image/gif"); imagegif($img); } } $image_name = $_GET['name']; resize_image($image_name ) ; ?> Hey, i'm having some trouble with this code I've written. It works when using remote website's images, though wont work when i point it to files on my host. Took me a while to write the code (im a noob), but cannot get it to work. Any suggestions? ps: It'a also kinda slow, maybe it will speed up when using local content/images, though im not sure? Is this a bad solution in your opinions? Thanks. Edited July 13, 2022 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/ Share on other sites More sharing options...
mac_gyver Posted July 13, 2022 Share Posted July 13, 2022 one = is an assignment operator. two == is a comparison operator. your if ($path_parts['extension'] = "jpg") { statements are assigning the string on the right to the $path_parts['extension'] variable, then testing the result, which will be a true value, so, each comparison will always be true. Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/#findComment-1598186 Share on other sites More sharing options...
oz11 Posted July 13, 2022 Author Share Posted July 13, 2022 Hey mac_gyver. Thanks for the feedback.. I've changed the code a bit and swapped stuff around. Tho now its just showing up as white squares. Not sure where to go from here. <?php function resize_image($image_name) { $path_parts = pathinfo($image_name); $path_parts['extension']; //$image_name = 'https://i.kym-cdn.com/photos/images/newsfeed/001/708/961/9e1.png'; if ($path_parts['extension'] == "jpg") { echo "jpeg"; $image = imagecreatefromjpeg($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] == "png"){ echo "png ;)"; $image = imagecreatefrompng($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] == "gif"){ echo "gif ;)"; $image = imagecreatefromgif($image_name); $img = imagescale( $image, 500, 400 ); header("Content-type: image/gif"); imagegif($img); } } resize_image($_GET['name']) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/#findComment-1598187 Share on other sites More sharing options...
Solution Barand Posted July 13, 2022 Solution Share Posted July 13, 2022 Remove those echo "jpeg"; etc. You can't have output before a header(); Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/#findComment-1598188 Share on other sites More sharing options...
oz11 Posted July 13, 2022 Author Share Posted July 13, 2022 omg. it worked. thanks ALOT comrade. Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/#findComment-1598189 Share on other sites More sharing options...
oz11 Posted July 13, 2022 Author Share Posted July 13, 2022 Just going to place this here also... <?php function resize_image($image_name) { //$image_name = 'https://c.tenor.com/GnSnviXkCR4AAAAd/memes.gif'; $image_name = $image_name; $path_parts = pathinfo($image_name); $path_parts['extension']; if ($path_parts['extension'] == "jpg") { //echo "jpeg"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefromjpeg($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/jpeg"); imagejpeg($img); } if ($path_parts['extension'] == "png"){ //echo "png ;)"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefrompng($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/png"); imagepng($img); } if ($path_parts['extension'] == "gif"){ //echo "gif ;)"; list($width, $height) = getimagesize($image_name); $new_width = 300; $new_height = ($height/$width)*$new_width; $image = imagecreatefromgif($image_name); $img = imagescale( $image, $new_width, $new_height ); header("Content-type: image/gif"); imagegif($img); } // Later support } resize_image("https://c.tenor.com/GnSnviXkCR4AAAAd/memes.gif") ; ?> This uses the aspect ratio of original file. Enjoi Quote Link to comment https://forums.phpfreaks.com/topic/315038-php-image-scaler-supporting-gif-png-and-jpeg/#findComment-1598190 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.