Ejemplo n.º 1
0
 /**
  * alternative workaround function for bicubic resizing. works well for downsizing and upsizing. VERY VERY slow. taken from php.net comments
  *
  * @access private
  */
 function _imageCopyResampledWorkaround2(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
 {
     ImagePaletteCopy($dst_img, $src_img);
     $rX = $src_w / $dst_w;
     $rY = $src_h / $dst_h;
     $w = 0;
     for ($y = $dst_y; $y < $dst_h; $y++) {
         $ow = $w;
         $w = round(($y + 1) * $rY);
         $t = 0;
         for ($x = $dst_x; $x < $dst_w; $x++) {
             $r = $g = $b = 0;
             $a = 0;
             $ot = $t;
             $t = round(($x + 1) * $rX);
             for ($u = 0; $u < $w - $ow; $u++) {
                 for ($p = 0; $p < $t - $ot; $p++) {
                     $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u));
                     $r += $c['red'];
                     $g += $c['green'];
                     $b += $c['blue'];
                     $a++;
                 }
             }
             ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
         }
     }
 }
Ejemplo n.º 2
0
 public static function beResizeImg($isrc, $idst, $iw, $ih, $ip = 'keep', $imax = 'yes', $iqual = 95)
 {
     /* parameters
        $isrc					source file
        $idst					destination folder
        $iw						new width
        $ih						new height
        $ip='keep'		keep proportions
        $imax='yes'		treat width/height as maximums
        $iqual=95			image quality
        */
     //read array with source image information
     $imagedata = getimagesize($isrc);
     //calculate new width and height
     $srcwid = $imagedata[0];
     $srchei = $imagedata[1];
     $srcx = $srcy = 0;
     //if no height is set we calculate it
     if ($ih == "full") {
         $ih = (int) ($imagedata[1] * ($iw / $imagedata[0]));
     }
     //the new width
     $new_iw = (int) $iw;
     //the new height
     if ($ip == 'keep') {
         //keep proportions
         $new_ih = (int) ($imagedata[1] * ($new_iw / $imagedata[0]));
         if ($new_ih > $ih) {
             $new_ih = (int) $ih;
             $new_iw = (int) ($imagedata[0] * ($new_ih / $imagedata[1]));
         }
     } else {
         //set fixed height
         $new_ih = (int) $ih;
         //crop
         if ($imagedata[0] / $new_iw < $imagedata[1] / $new_ih) {
             //original to heigh
             $srchei = (int) ($new_ih * ($imagedata[0] / $new_iw));
             $srcy = (int) (($imagedata[1] - $srchei) / 2);
         } elseif ($imagedata[0] / $new_iw > $imagedata[1] / $new_ih) {
             //original to wide
             $srcwid = (int) ($new_iw * ($imagedata[1] / $new_ih));
             $srcx = (int) (($imagedata[0] - $srcwid) / 2);
         }
     }
     //set image type and set image name
     $ipath = pathinfo($isrc);
     $itype = strtolower($ipath["extension"]);
     $iname = substr($ipath["basename"], 0, -(strlen($itype) + 1)) . "_" . $new_iw . "_" . $new_ih . "_" . $iqual . "." . $itype;
     //check if $idst is set to a subdirectory and if so add it to the path
     $idir = $idst != "" ? $ipath["dirname"] . "/" . $idst : $ipath["dirname"];
     //check if image exists, else create it
     if (!JFile::exists($idir . "/" . $iname)) {
         if ($itype == "jpg" && function_exists("imagecreatefromjpeg")) {
             $image = imagecreatefromjpeg($isrc);
             $image_dest = imagecreatetruecolor($new_iw, $new_ih);
             imagecopyresampled($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei);
             ob_start();
             // start a new output buffer
             imagejpeg($image_dest, NULL, $iqual);
             $buffer = ob_get_contents();
             ob_end_clean();
             // stop this output buffer
         } elseif ($itype == "gif" && function_exists("imagecreatefromgif")) {
             $image = imagecreatefromgif($isrc);
             $image_dest = imagecreatetruecolor($new_iw, $new_ih);
             imagealphablending($image_dest, false);
             // get and reallocate transparency-color
             $transindex = imagecolortransparent($image);
             if ($transindex >= 0) {
                 $transcol = imagecolorsforindex($image, $transindex);
                 $transindex = imagecolorallocatealpha($image_dest, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
                 imagefill($image_dest, 0, 0, $transindex);
             }
             // resample
             imagecopyresampled($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei);
             // restore transparency
             if ($transindex >= 0) {
                 imagecolortransparent($image_dest, $transindex);
                 for ($y = 0; $y < $new_ih; ++$y) {
                     for ($x = 0; $x < $new_iw; ++$x) {
                         if ((imagecolorat($image_dest, $x, $y) >> 24 & 0x7f) >= 100) {
                             imagesetpixel($image_dest, $x, $y, $transindex);
                         }
                     }
                 }
             }
             imagetruecolortopalette($image_dest, true, 255);
             imagesavealpha($image_dest, false);
             ob_start();
             // start a new output buffer
             imagegif($image_dest, NULL, $iqual);
             $buffer = ob_get_contents();
             ob_end_clean();
             // stop this output buffer
         } elseif ($itype == "png" && function_exists("imagecreatefrompng")) {
             $image = ImageCreateFromPng($isrc);
             $image_dest = ImageCreateTrueColor($new_iw, $new_ih);
             $transindex = imagecolortransparent($image);
             $istruecolor = imageistruecolor($image);
             if ($transindex >= 0) {
                 ImageColorTransparent($image_dest, ImageColorAllocate($image_dest, 0, 0, 0));
                 ImageAlphaBlending($image_dest, false);
             } elseif (!$istruecolor) {
                 ImagePaletteCopy($image_dest, $image);
             } else {
                 ImageColorTransparent($image_dest, ImageColorAllocate($image_dest, 0, 0, 0));
                 ImageAlphaBlending($image_dest, false);
                 ImageSaveAlpha($image_dest, true);
             }
             ImageCopyResized($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei);
             $iqual_png = 100 - $iqual;
             if (substr(phpversion(), 0, 1) >= 5) {
                 $iqual_png = intval(($iqual - 10) / 10);
             }
             ob_start();
             // start a new output buffer
             Imagepng($image_dest, NULL, $iqual_png);
             $buffer = ob_get_contents();
             ob_end_clean();
             // stop this output buffer
         }
         if (isset($buffer) && $buffer != "") {
             JFile::write($idir . '/' . $iname, $buffer);
             unset($buffer);
         }
         if (isset($image)) {
             imagedestroy($image);
         }
         if (isset($image_dest)) {
             imagedestroy($image_dest);
         }
     }
     //utf8_encode and rawurlencode file name
     $iname = rawurlencode(utf8_encode($iname));
     //return path/filename/type/width/height
     return $thenewimage = array($idir . '/' . $iname, $iname, $itype, $new_iw, $new_ih);
 }
Ejemplo n.º 3
0
        $img = Imagegif($dst_img, '', $_quality_);
        $ob_contents = ob_get_contents();
        // Save file
        $fp = fopen("{$path}", 'wb');
        fwrite($fp, $ob_contents);
        fclose($fp);
        ob_end_flush();
    }
}
if (substr($_GET['img'], -3) == "png") {
    header("Content-type: image/png");
    if (file_exists($path)) {
        echo file_get_contents($path);
    } else {
        if (strnatcmp(phpversion(), '4.3.0') > 0) {
            $_quality_ = (int) ($_quality_ / 10);
        }
        $src_img = ImageCreateFromPng($_image_);
        $dst_img = imagecreatetruecolor($new_w, $new_h);
        ImagePaletteCopy($dst_img, $src_img);
        ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img));
        ob_start();
        $img = Imagepng($dst_img, '', $_quality_);
        $ob_contents = ob_get_contents();
        // Save file
        $fp = fopen("{$path}", 'wb');
        fwrite($fp, $ob_contents);
        fclose($fp);
        ob_end_flush();
    }
}
Ejemplo n.º 4
0
 public static function imageCopyResampledBicubic(&$dst_image, &$src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
 {
     // We should first cut the piece we are interested in from the source
     $src_img = ImageCreateTrueColor($src_w, $src_h);
     imagecopy($src_img, $src_image, 0, 0, $src_x, $src_y, $src_w, $src_h);
     // This one is used as temporary image
     $dst_img = ImageCreateTrueColor($dst_w, $dst_h);
     ImagePaletteCopy($dst_img, $src_img);
     $rX = $src_w / $dst_w;
     $rY = $src_h / $dst_h;
     $w = 0;
     for ($y = 0; $y < $dst_h; $y++) {
         $ow = $w;
         $w = round(($y + 1) * $rY);
         $t = 0;
         for ($x = 0; $x < $dst_w; $x++) {
             $r = $g = $b = 0;
             $a = 0;
             $ot = $t;
             $t = round(($x + 1) * $rX);
             for ($u = 0; $u < $w - $ow; $u++) {
                 for ($p = 0; $p < $t - $ot; $p++) {
                     $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u));
                     $r += $c['red'];
                     $g += $c['green'];
                     $b += $c['blue'];
                     $a++;
                 }
             }
             ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
         }
     }
     // Apply the temp image over the returned image and use the destination x,y coordinates
     imagecopy($dst_image, $dst_img, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h);
     // We should return true since ImageCopyResampled/ImageCopyResized do it
     return true;
 }
 /**
  * resizes an image using several different techniques:
  *
  * PHP's own ImageCopyResamplated
  * Bi-linear filter (slower, but better quality than ImageCopyResampled)
  * Bi-Cubic filter (slowest, but offers the best quality)
  * PHP's own ImageCopyResized (fastest one, but offers no antialising or filter)
  *
  */
 function ImageResize($dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $resample = GD_RESIZER_NO_SMOOTHING_MODE)
 {
     $pxls = intval($src_w / $dst_w) - 1;
     if ($resample == GD_RESIZER_PHP_IMAGECOPYRESAMPLED) {
         imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
     } elseif ($resample == GD_RESIZER_BILINEAR_MODE) {
         //slow but better quality
         ImageTrueColorToPalette($src_img, false, 256);
         ImagePaletteCopy($dst_img, $src_img);
         $rX = $src_w / $dst_w;
         $rY = $src_h / $dst_h;
         $nY = 0;
         for ($y = $src_y; $y < $dst_h; $y++) {
             $oY = $nY;
             $nY = intval(($y + 1) * $rY + 0.5);
             $nX = 0;
             for ($x = $src_x; $x < $dst_w; $x++) {
                 $r = $g = $b = $a = 0;
                 $oX = $nX;
                 $nX = intval(($x + 1) * $rX + 0.5);
                 $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX, $nY));
                 $r += $c['red'];
                 $g += $c['green'];
                 $b += $c['blue'];
                 $a++;
                 $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX - $pxls, $nY - $pxls));
                 $r += $c['red'];
                 $g += $c['green'];
                 $b += $c['blue'];
                 $a++;
                 //you can add more pixels here! eg "$nX, $nY-$pxls" or "$nX-$pxls, $nY"
                 ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
             }
         }
     } elseif ($resample == GD_RESIZER_BICUBIC_MODE) {
         // veeeeeery slow but better quality
         ImagePaletteCopy($dst_img, $src_img);
         $rX = $src_w / $dst_w;
         $rY = $src_h / $dst_h;
         $nY = 0;
         for ($y = $src_y; $y < $dst_h; $y++) {
             $oY = $nY;
             $nY = intval(($y + 1) * $rY + 0.5);
             $nX = 0;
             for ($x = $src_x; $x < $dst_w; $x++) {
                 $r = $g = $b = $a = 0;
                 $oX = $nX;
                 $nX = intval(($x + 1) * $rX + 0.5);
                 for ($i = $nY; --$i >= $oY;) {
                     for ($j = $nX; --$j >= $oX;) {
                         $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $j, $i));
                         $r += $c['red'];
                         $g += $c['green'];
                         $b += $c['blue'];
                         $a++;
                     }
                 }
                 ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
             }
         }
     } else {
         $dst_w++;
         $dst_h++;
         //->no black border
         imagecopyresized($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
     }
 }