Jump to content

php image scaler, (supporting "gif", "png" and "jpeg")


oz11
Go to solution Solved by Barand,

Recommended Posts

<?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 by oz11
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']) ;

?> 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.