Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resize and convert an uploaded image to a PNG using GD?

I want to allow users to upload avatar-type images in a variety of formats (GIF, JPEG, and PNG at least), but to save them all as PNG database BLOBs. If the images are oversized, pixelwise, I want to resize them before DB-insertion.

What is the best way to use GD to do the resizing and PNG conversion?

Edit: Sadly, only GD is available on the server I need to use, no ImageMagick.

like image 829
Cheekysoft Avatar asked Aug 22 '08 12:08

Cheekysoft


People also ask

How do I convert an image to PNG?

Converting an Image With WindowsOpen the image you want to convert into PNG by clicking File > Open. Navigate to your image and then click “Open.” Once the file is open, click File > Save As. In the next window make sure you have PNG selected from the drop-down list of formats, and then click “Save.”

Can you scale PNG?

To resize a PNG file without losing quality, use a lossless compression tool that supports transparency and PNG files. There are many compression tools available online that can do this for free.


2 Answers

<?php                                              
/*
Resizes an image and converts it to PNG returning the PNG data as a string
*/
function imageToPng($srcFile, $maxSize = 100) {  
    list($width_orig, $height_orig, $type) = getimagesize($srcFile);        

    // Get the aspect ratio
    $ratio_orig = $width_orig / $height_orig;

    $width  = $maxSize; 
    $height = $maxSize;

    // resize to height (orig is portrait) 
    if ($ratio_orig < 1) {
        $width = $height * $ratio_orig;
    } 
    // resize to width (orig is landscape)
    else {
        $height = $width / $ratio_orig;
    }

    // Temporarily increase the memory limit to allow for larger images
    ini_set('memory_limit', '32M'); 

    switch ($type) 
    {
        case IMAGETYPE_GIF: 
            $image = imagecreatefromgif($srcFile); 
            break;   
        case IMAGETYPE_JPEG:  
            $image = imagecreatefromjpeg($srcFile); 
            break;   
        case IMAGETYPE_PNG:  
            $image = imagecreatefrompng($srcFile);
            break; 
        default:
            throw new Exception('Unrecognized image type ' . $type);
    }

    // create a new blank image
    $newImage = imagecreatetruecolor($width, $height);

    // Copy the old image to the new image
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output to a temp file
    $destFile = tempnam();
    imagepng($newImage, $destFile);  

    // Free memory                           
    imagedestroy($newImage);

    if ( is_file($destFile) ) {
        $f = fopen($destFile, 'rb');   
        $data = fread($f);       
        fclose($f);

        // Remove the tempfile
        unlink($destFile);    
        return $data;
    }

    throw new Exception('Image conversion failed.');
}
like image 154
Acuminate Avatar answered Nov 05 '22 20:11

Acuminate


Your process steps should look like this:

  1. Verify the filetype
  2. Load the image if it is a supported filetype into GD using imagecreatefrom*
  3. Resizing using imagecopyresize or imagecopyresampled
  4. Save the image using imagepng($handle, 'filename.png', $quality, $filters)

ImageMagick is faster, generates better images, is more configurable, and finally is (IMO) much easier to code for.

@ceejayoz Just wait for the new GD - it's OOP like MySQLi and it's actually not bad :)

like image 38
Ross Avatar answered Nov 05 '22 20:11

Ross