#1
  1. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785

    Help with GD library and imagexxxx functions


    Hello,

    I am modifing our imaging class and a method called convertAndScale. Unfortunately this code will only appropriately work with JPG files. If I try to do a PNG file the resulting image is corrupted. Any ideas why? Initially I had thought it was due to my passing in the wrong 'quality' argument to the imagepng method but unfortunately the problem persists.

    PHP Code:
      static function convertAndScale ($sInputFile$sOutputFile$iTargetWidth 0$iTargetHeight 0$sTargetFileFormat "JPEG") {
        
        
    // Init
        
    $bResult true;
        if (!
    file_exists ($sInputFile))
          return (
    false);
            
        
    // Determine source image type
        
    $iImageType exif_imagetype ($sInputFile);
        if (
    $iImageType != IMAGETYPE_GIF &&
            
    $iImageType != IMAGETYPE_JPEG &&  
            
    $iImageType != IMAGETYPE_PNG)
          return (
    false);

        
    // Load image from file
        
    if ($iImageType == IMAGETYPE_GIF)
          
    $oInput imagecreatefromgif ($sInputFile);
        else if (
    $iImageType == IMAGETYPE_JPEG)
          
    $oInput imagecreatefromjpeg ($sInputFile);
        else if (
    $iImageType == IMAGETYPE_PNG)
          
    $oInput imagecreatefrompng ($sInputFile);

        
    // Prepare canvas
        
    list ($iSourceWidth$iSourceHeight) = getimagesize ($sInputFile);
        
        
    // Scale image (if target width & height > 0)
        
    if ($iTargetWidth 0) {
          
    $aNewSize self::GetBestFitImageSize ($iSourceWidth$iSourceHeight$iTargetWidth$iTargetHeight);
          
    $oOutputImg imagecreatetruecolor ($aNewSize ["iImageWidth"], $aNewSize ["iImageHeight"]);
      
          
    $white imagecolorallocate ($oOutputImg,  255255255);
          
    imagefilledrectangle ($oOutputImg00$aNewSize ["iImageWidth"], $aNewSize ["iImageHeight"], $white);
      
          
    imagecopyresampled ($oOutputImg$oInput0000,
                              
    $aNewSize ["iImageWidth"], $aNewSize ["iImageHeight"],
                              
    $iSourceWidth$iSourceHeight);

          
    // Output image to file to specified output format
          
    $bResult self::saveFileFormat ($oOutputImg$sOutputFile$sTargetFileFormat);
        } else {
          
    // Otherwise just convert it to specified output format 
          
    $bResult self::saveFileFormat ($oInput$sOutputFile$sTargetFileFormat);
        }

        
    // Return results
        
    return ($bResult);
      }  

      private static function 
    saveFileFormat ($oInput$sOutputFile$sFormat "JPEG") {
     
        
    $sFormat strtoupper($sFormat);

        
    // Save file and ensure output file extension is correct
        
    if ($sFormat == "GIF") {
          
    $sOutputFile preg_replace('"\.[a-zA-Z]$"''.gif'$sOutputFile);
          
    $bResult imagegif ($oInput$sOutputFile);
        } elseif (
    $sFormat == "PNG") {
          
    $sOutputFile preg_replace('"\.[a-zA-Z]$"''.png'$sOutputFile);
          
    $bResult imagepng ($oInput$sOutputFile0);
        } else {
          
    $sOutputFile preg_replace('"\.[a-zA-Z]$"''.jpg'$sOutputFile);
          
    $bResult imagejpeg ($oInput$sOutputFile85);
        }
        
        
    imagedestroy($oInput);

        
    // Return results
        
    return $bResult;

      } 
    Thanks in advance for any input. This has been a struggle.
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,112
    Rep Power
    9398
  4. #3
  5. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    Its very possible the image was being cached by the browser - after some time and trying it again to get the error'd image it now works properly.

    I apologize for the gaffe. If it starts acting up again I'll come back to be sure
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded

IMN logo majestic logo threadwatch logo seochat tools logo