예제 #1
0
 private function resample(&$img, ImageManipulator $source, $owdt, $ohgt, $maxwdt, $maxhgt, $quality = 1)
 {
     // make sure the image doesn't get enlarged
     $maxwdt = min($maxwdt, $owdt);
     $maxhgt = min($maxhgt, $ohgt);
     if (!$maxwdt) {
         $divwdt = 1;
     } else {
         $divwdt = max(1, $owdt / $maxwdt);
     }
     if (!$maxhgt) {
         $divhgt = 1;
     } else {
         $divhgt = max(1, $ohgt / $maxhgt);
     }
     if ($divwdt >= $divhgt) {
         $newwdt = round($owdt / $divwdt);
         $newhgt = round($ohgt / $divwdt);
     } else {
         $newhgt = round($ohgt / $divhgt);
         $newwdt = round($owdt / $divhgt);
     }
     // return same image if resizing is not necessary
     if ($newwdt == $owdt && $newhgt == $ohgt) {
         return false;
     }
     $tn = imagecreatetruecolor($newwdt, $newhgt);
     if (in_array($source->getType(), array(IMAGETYPE_GIF, IMAGETYPE_PNG))) {
         $trnprt_indx = imagecolortransparent($img);
         // If we have a specific transparent color
         if ($trnprt_indx >= 0) {
             // Get the original image's transparent color's RGB values
             $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
             // Allocate the same color in the new image resource
             $trnprt_indx = imagecolorallocate($tn, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
             // Completely fill the background of the new image with allocated color.
             imagefill($tn, 0, 0, $trnprt_indx);
             // Set the background color for new image to transparent
             imagecolortransparent($tn, $trnprt_indx);
         } elseif ($source->getType() == IMAGETYPE_PNG) {
             // Turn off transparency blending (temporarily)
             imagealphablending($tn, false);
             // Create a new transparent color for image
             $color = imagecolorallocatealpha($tn, 0, 0, 0, 127);
             // Completely fill the background of the new image with allocated color.
             imagefill($tn, 0, 0, $color);
             // Restore transparency blending
             imagesavealpha($tn, true);
         }
     }
     if ($quality) {
         imagecopyresampled($tn, $img, 0, 0, 0, 0, $newwdt, $newhgt, $owdt, $ohgt);
     } else {
         imagecopyresized($tn, $img, 0, 0, 0, 0, $newwdt, $newhgt, $owdt, $ohgt);
     }
     imagedestroy($img);
     $img = $tn;
     return array($newwdt, $newhgt);
 }