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

前端 未结 9 1393
离开以前
离开以前 2020-12-05 16:08

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

相关标签:
9条回答
  • 2020-12-05 16:53
    <?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.');
    }
    
    0 讨论(0)
  • 2020-12-05 16:57

    I think this page is a good starting point. It uses imagecreatefrom(jpeg/gif/png) and resize and converts the image and then outputs to the browser. Instead of outputting the browser you could output to a BLOB in a DB without many minuttes of code-rewrite.

    0 讨论(0)
  • 2020-12-05 16:59

    Something like this, perhaps:

    
    <?php
       //Input file
       $file = "myImage.png";
       $img = ImageCreateFromPNG($file);
    
       //Dimensions
       $width = imagesx($img);
       $height = imagesy($img);
       $max_width = 300;
       $max_height = 300;
       $percentage = 1;
    
       //Image scaling calculations
       if ( $width > $max_width ) { 
          $percentage = ($height / ($width / $max_width)) > $max_height ?
               $height / $max_height :
               $width / $max_width;
       }
       elseif ( $height > $max_height) {
          $percentage = ($width / ($height / $max_height)) > $max_width ? 
               $width / $max_width :
               $height / $max_height;
       }
       $new_width = $width / $percentage;
       $new_height = $height / $percentage;
    
       //scaled image
       $out = imagecreatetruecolor($new_width, $new_height);
       imagecopyresampled($out, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
       //output image
       imagepng($out);
    ?>
    

    I haven't tested the code so there might be some syntax errors, however it should give you a fair presentation on how it could be done. Also, I assumed a PNG file. You might want to have some kind of switch statement to determine the file type.

    0 讨论(0)
提交回复
热议问题