Example #1
0
 function applyFilter(Canvas $canvas)
 {
     $himage = $canvas->getImage();
     if (function_exists('imagefilter')) {
         // If gd is bundled this will work
         imagefilter($himage, IMG_FILTER_GRAYSCALE);
     } else {
         // If not we need to do some enumeration
         $total = imagecolorstotal($himage);
         if ($total > 0) {
             // This works for indexed images but not for truecolor
             for ($i = 0; $i < $total; $i++) {
                 $index = imagecolorsforindex($himage, $i);
                 $avg = ($index["red"] + $index["green"] + $index["blue"]) / 3;
                 $red = $avg;
                 $green = $avg;
                 $blue = $avg;
                 imagecolorset($himage, $i, $red, $green, $blue);
             }
         } else {
             // For truecolor we need to enum it all
             for ($x = 0; $x < imagesx($himage); $x++) {
                 for ($y = 0; $y < imagesy($himage); $y++) {
                     $index = imagecolorat($himage, $x, $y);
                     $avg = (($index & 0xff) + ($index >> 8 & 0xff) + ($index >> 16 & 0xff)) / 3;
                     imagesetpixel($himage, $x, $y, $avg | $avg << 8 | $avg << 16);
                 }
             }
         }
     }
 }
Example #2
0
/**
 *
 * long description
 * @global object
 * @param object $dst_img
 * @param object $src_img
 * @param int $dst_x 
 * @param int $dst_y 
 * @param int $src_x 
 * @param int $src_y 
 * @param int $dst_w 
 * @param int $dst_h 
 * @param int $src_w 
 * @param int $src_h 
 * @return bool
 * @todo Finish documenting this function
 */
function ImageCopyBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
    global $CFG;
    if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) {
        return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    }
    $totalcolors = imagecolorstotal($src_img);
    for ($i = 0; $i < $totalcolors; $i++) {
        if ($colors = ImageColorsForIndex($src_img, $i)) {
            ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
        }
    }
    $scaleX = ($src_w - 1) / $dst_w;
    $scaleY = ($src_h - 1) / $dst_h;
    $scaleX2 = $scaleX / 2.0;
    $scaleY2 = $scaleY / 2.0;
    for ($j = 0; $j < $dst_h; $j++) {
        $sY = $j * $scaleY;
        for ($i = 0; $i < $dst_w; $i++) {
            $sX = $i * $scaleX;
            $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2));
            $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY));
            $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2));
            $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY));
            $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
            $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
            $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
            $color = ImageColorClosest($dst_img, $red, $green, $blue);
            ImageSetPixel($dst_img, $i + $dst_x, $j + $dst_y, $color);
        }
    }
}
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function analyze($filename)
 {
     $imageSize = @getimagesize($filename);
     if ($imageSize === false) {
         throw new UnsupportedFileException('File type not supported.');
     }
     $imageInfo = new ImageInfo();
     $type = null;
     $colors = null;
     if ($imageSize[2] === IMAGETYPE_JPEG) {
         $gd = imagecreatefromjpeg($filename);
         $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE';
         $colors = imagecolorstotal($gd);
         imagedestroy($gd);
     } elseif ($imageSize[2] === IMAGETYPE_GIF) {
         $gd = imagecreatefromgif($filename);
         $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE';
         $colors = imagecolorstotal($gd);
         imagedestroy($gd);
     } elseif ($imageSize[2] === IMAGETYPE_PNG) {
         $gd = imagecreatefrompng($filename);
         $type = imageistruecolor($gd) ? 'TRUECOLOR' : 'PALETTE';
         $colors = imagecolorstotal($gd);
         imagedestroy($gd);
     }
     $imageInfo->setAnalyzer(get_class($this))->setSize($imageSize[0], $imageSize[1])->setResolution(null, null)->setUnits(null)->setFormat($this->mapFormat($imageSize[2]))->setColors($colors)->setType($type)->setColorspace(!empty($imageSize['channels']) ? $imageSize['channels'] === 4 ? 'CMYK' : 'RGB' : 'RGB')->setDepth($imageSize['bits'])->setCompression(null)->setQuality(null)->setProfiles(null);
     return $imageInfo;
 }
 function execute()
 {
     $img =& $this->image->getImage();
     if (!($t = imagecolorstotal($img))) {
         $t = 256;
         imagetruecolortopalette($img, true, $t);
     }
     $total = imagecolorstotal($img);
     for ($i = 0; $i < $total; $i++) {
         $index = imagecolorsforindex($img, $i);
         $red = $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189;
         $green = $index["red"] * 0.349 + $index["green"] * 0.6860000000000001 + $index["blue"] * 0.168;
         $blue = $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131;
         if ($red > 255) {
             $red = 255;
         }
         if ($green > 255) {
             $green = 255;
         }
         if ($blue > 255) {
             $blue = 255;
         }
         imagecolorset($img, $i, $red, $green, $blue);
     }
 }
Example #5
0
 /**
  * 透過処理済みの新しいイメージオブジェクトを生成する。
  */
 function transparent($image, $info, $newImage = null)
 {
     if ($newImage == null) {
         $newImage = imagecreatetruecolor($this->width, $this->height);
     }
     if ($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG) {
         // 元画像の透過色を取得する。
         $trnprt_indx = imagecolortransparent($image);
         // パレットサイズを取得する。
         $palletsize = imagecolorstotal($image);
         // 透過色が設定されている場合は透過処理を行う。
         if ($trnprt_indx >= 0 && $trnprt_indx < $palletsize) {
             // カラーインデックスから透過色を取得する。
             $trnprt_color = imagecolorsforindex($image, $trnprt_indx);
             // 取得した透過色から変換後の画像用のカラーインデックスを生成
             $trnprt_indx = imagecolorallocate($newImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
             // 生成した透過色で変換後画像を塗りつぶし
             imagefill($newImage, 0, 0, $trnprt_indx);
             // 生成した透過色を変換後画像の透過色として設定
             imagecolortransparent($newImage, $trnprt_indx);
         } elseif ($info[2] == IMAGETYPE_PNG) {
             // アルファブレンディングをOFFにする。
             imagealphablending($newImage, false);
             // アルファブレンドのカラーを作成する。
             $trnprt_indx = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
             // 生成した透過色で変換後画像を塗りつぶし
             imagefill($newImage, 0, 0, $trnprt_indx);
             // 透過色をGIF用に設定
             imagecolortransparent($newImage, $trnprt_indx);
             // 生成した透過色を変換後画像の透過色として設定
             imagesavealpha($newImage, true);
         }
     }
     return $newImage;
 }
 /**
  * Convert an image.
  *
  * @param   img.Image image
  * @return  bool
  * @throws  img.ImagingException
  */
 public function convert($image)
 {
     // Create temporary variable as local variable access is faster
     // than member variable access.
     $handle = $image->handle;
     if (imageistruecolor($handle)) {
         $l = [];
         $h = $image->getHeight();
         $w = $image->getWidth();
         for ($y = 0; $y < $h; $y++) {
             for ($x = 0; $x < $w; $x++) {
                 $rgb = imagecolorat($handle, $x, $y);
                 if (!isset($l[$rgb])) {
                     $g = 0.299 * ($rgb >> 16 & 0xff) + 0.587 * ($rgb >> 8 & 0xff) + 0.114 * ($rgb & 0xff);
                     $l[$rgb] = imagecolorallocate($handle, $g, $g, $g);
                 }
                 imagesetpixel($handle, $x, $y, $l[$rgb]);
             }
         }
         unset($l);
     } else {
         for ($i = 0, $t = imagecolorstotal($handle); $i < $t; $i++) {
             $c = imagecolorsforindex($handle, $i);
             $g = 0.299 * $c['red'] + 0.587 * $c['green'] + 0.114 * $c['blue'];
             imagecolorset($handle, $i, $g, $g, $g);
         }
     }
 }
Example #7
0
/**
 * Copies a rectangular portion of the source image to another rectangle in the destination image
 *
 * This function calls imagecopyresampled() if it is available and GD version is 2 at least.
 * Otherwise it reimplements the same behaviour. See the PHP manual page for more info.
 *
 * @link http://php.net/manual/en/function.imagecopyresampled.php
 * @param resource $dst_img the destination GD image resource
 * @param resource $src_img the source GD image resource
 * @param int $dst_x vthe X coordinate of the upper left corner in the destination image
 * @param int $dst_y the Y coordinate of the upper left corner in the destination image
 * @param int $src_x the X coordinate of the upper left corner in the source image
 * @param int $src_y the Y coordinate of the upper left corner in the source image
 * @param int $dst_w the width of the destination rectangle
 * @param int $dst_h the height of the destination rectangle
 * @param int $src_w the width of the source rectangle
 * @param int $src_h the height of the source rectangle
 * @return bool tru on success, false otherwise
 */
function imagecopybicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
    global $CFG;
    if (function_exists('imagecopyresampled') and $CFG->gdversion >= 2) {
        return imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    }
    $totalcolors = imagecolorstotal($src_img);
    for ($i = 0; $i < $totalcolors; $i++) {
        if ($colors = imagecolorsforindex($src_img, $i)) {
            imagecolorallocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
        }
    }
    $scalex = ($src_w - 1) / $dst_w;
    $scaley = ($src_h - 1) / $dst_h;
    $scalex2 = $scalex / 2.0;
    $scaley2 = $scaley / 2.0;
    for ($j = 0; $j < $dst_h; $j++) {
        $sy = $j * $scaley;
        for ($i = 0; $i < $dst_w; $i++) {
            $sx = $i * $scalex;
            $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx, (int) $sy + $scaley2));
            $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx, (int) $sy));
            $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx + $scalex2, (int) $sy + $scaley2));
            $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx + $scalex2, (int) $sy));
            $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
            $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
            $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
            $color = imagecolorclosest($dst_img, $red, $green, $blue);
            imagesetpixel($dst_img, $i + $dst_x, $j + $dst_y, $color);
        }
    }
}
Example #8
0
 /**
  * Fit small image to specified bound
  *
  * @param string $src
  * @param string $dest
  * @param int $width
  * @param int $height
  * @return bool
  */
 public function fit($src, $dest, $width, $height)
 {
     // Calculate
     $size = getimagesize($src);
     $ratio = max($width / $size[0], $height / $size[1]);
     $old_width = $size[0];
     $old_height = $size[1];
     $new_width = intval($old_width * $ratio);
     $new_height = intval($old_height * $ratio);
     // Resize
     @ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT));
     $image = imagecreatefromstring(file_get_contents($src));
     $new_image = wp_imagecreatetruecolor($new_width, $new_height);
     imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
     if (IMAGETYPE_PNG == $size[2] && function_exists('imageistruecolor') && !imageistruecolor($image)) {
         imagetruecolortopalette($new_image, false, imagecolorstotal($image));
     }
     // Destroy old image
     imagedestroy($image);
     // Save
     switch ($size[2]) {
         case IMAGETYPE_GIF:
             $result = imagegif($new_image, $dest);
             break;
         case IMAGETYPE_PNG:
             $result = imagepng($new_image, $dest);
             break;
         default:
             $result = imagejpeg($new_image, $dest);
             break;
     }
     imagedestroy($new_image);
     return $result;
 }
Example #9
0
 public function execute()
 {
     $this->media->asImage();
     $img = $this->media->getImage();
     if (!($t = imagecolorstotal($img))) {
         $t = 256;
         imagetruecolortopalette($img, true, $t);
     }
     $total = imagecolorstotal($img);
     for ($i = 0; $i < $total; ++$i) {
         $index = imagecolorsforindex($img, $i);
         $red = $index['red'] * 0.393 + $index['green'] * 0.769 + $index['blue'] * 0.189;
         $green = $index['red'] * 0.349 + $index['green'] * 0.6860000000000001 + $index['blue'] * 0.168;
         $blue = $index['red'] * 0.272 + $index['green'] * 0.534 + $index['blue'] * 0.131;
         if ($red > 255) {
             $red = 255;
         }
         if ($green > 255) {
             $green = 255;
         }
         if ($blue > 255) {
             $blue = 255;
         }
         imagecolorset($img, $i, $red, $green, $blue);
     }
     $this->media->setImage($img);
 }
Example #10
0
 function applyFilter(Canvas $canvas)
 {
     $himage = $canvas->getImage();
     // If not we need to do some enumeration
     $total = imagecolorstotal($himage);
     if ($total > 0) {
         // This works for indexed images but not for truecolor
         for ($i = 0; $i < $total; $i++) {
             $index = imagecolorsforindex($himage, $i);
             $rgb = rgb($index['red'], $index['green'], $index['blue']);
             $hsv = hsv($rgb);
             $hsv->hue = $this->hue;
             $rgb = rgb($hsv);
             $red = $rgb->red;
             $green = $rgb->green;
             $blue = $rgb->blue;
             imagecolorset($himage, $i, $red, $green, $blue);
         }
     } else {
         // For truecolor we need to enum it all
         for ($x = 0; $x < imagesx($himage); $x++) {
             for ($y = 0; $y < imagesy($himage); $y++) {
                 $index = imagecolorat($himage, $x, $y);
                 $rgb = rgb($index['red'], $index['green'], $index['blue'], $index['alpha']);
                 $hsv = hsv($rgb);
                 $hsv->hue = $this->hue;
                 $rgb = rgb($hsv);
                 $red = $rgb->red;
                 $green = $rgb->green;
                 $blue = $rgb->blue;
                 imagesetpixel($himage, $x, $y, $red << 16 | $green < 8 | $blue);
             }
         }
     }
 }
Example #11
0
 function getColors()
 {
     $colors = array();
     for ($i = 0; $i < imagecolorstotal($this->im); $i++) {
         $colors[] = imagecolorsforindex($this->im, $i);
     }
     return $colors;
 }
Example #12
0
 /**
  * This function is almost equal to the image_resize (native function of wordpress)
  */
 function image_resize($file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90)
 {
     $image = wp_load_image($file);
     if (!is_resource($image)) {
         return new WP_Error('error_loading_image', $image);
     }
     $size = @getimagesize($file);
     if (!$size) {
         return new WP_Error('invalid_image', __('Could not read image size'), $file);
     }
     list($orig_w, $orig_h, $orig_type) = $size;
     $dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar);
     if (!$dims) {
         $dims = array(0, 0, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h);
     }
     list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
     $newimage = imagecreatetruecolor($dst_w, $dst_h);
     imagealphablending($newimage, false);
     imagesavealpha($newimage, true);
     $transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
     imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent);
     imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
     // convert from full colors to index colors, like original PNG.
     if (IMAGETYPE_PNG == $orig_type && !imageistruecolor($image)) {
         imagetruecolortopalette($newimage, false, imagecolorstotal($image));
     }
     // we don't need the original in memory anymore
     imagedestroy($image);
     $info = pathinfo($dest_path);
     $dir = $info['dirname'];
     $ext = $info['extension'];
     $name = basename($dest_path, ".{$ext}");
     $destfilename = "{$dir}/{$name}.{$ext}";
     if (IMAGETYPE_GIF == $orig_type) {
         if (!imagegif($newimage, $destfilename)) {
             return new WP_Error('resize_path_invalid', __('Resize path invalid'));
         }
     } elseif (IMAGETYPE_PNG == $orig_type) {
         if (!imagepng($newimage, $destfilename)) {
             return new WP_Error('resize_path_invalid', __('Resize path invalid'));
         }
     } else {
         // all other formats are converted to jpg
         //Todo: add option for use progresive JPG
         //imageinterlace($newimage, true); //Progressive JPG
         if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
             return new WP_Error('resize_path_invalid', __('Resize path invalid'));
         }
     }
     imagedestroy($newimage);
     // Set correct file permissions
     $stat = stat(dirname($destfilename));
     $perms = $stat['mode'] & 0666;
     //same permissions as parent folder, strip off the executable bits
     @chmod($destfilename, $perms);
     return $destfilename;
 }
 protected function _save($image, $filename = null, $mime_type = null)
 {
     global $ewww_debug;
     if (!defined('EWWW_IMAGE_OPTIMIZER_DOMAIN')) {
         require_once plugin_dir_path(__FILE__) . 'ewww-image-optimizer.php';
     }
     if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) {
         ewww_image_optimizer_init();
     }
     list($filename, $extension, $mime_type) = $this->get_output_format($filename, $mime_type);
     if (!$filename) {
         $filename = $this->generate_filename(null, null, $extension);
     }
     if ('image/gif' == $mime_type) {
         if (!$this->make_image($filename, 'imagegif', array($image, $filename))) {
             return new WP_Error('image_save_error', __('Image Editor Save Failed'));
         }
     } elseif ('image/png' == $mime_type) {
         // convert from full colors to index colors, like original PNG.
         if (function_exists('imageistruecolor') && !imageistruecolor($image)) {
             imagetruecolortopalette($image, false, imagecolorstotal($image));
         }
         if (property_exists('WP_Image_Editor', 'quality')) {
             $compression_level = floor((101 - $this->quality) * 0.09);
             $ewww_debug .= "png quality = " . $this->quality . "<br>";
         } else {
             $compression_level = floor((101 - false) * 0.09);
         }
         if (!$this->make_image($filename, 'imagepng', array($image, $filename, $compression_level))) {
             return new WP_Error('image_save_error', __('Image Editor Save Failed'));
         }
     } elseif ('image/jpeg' == $mime_type) {
         if (method_exists($this, 'get_quality')) {
             if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, $this->get_quality()))) {
                 return new WP_Error('image_save_error', __('Image Editor Save Failed'));
             }
         } else {
             if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, apply_filters('jpeg_quality', $this->quality, 'image_resize')))) {
                 return new WP_Error('image_save_error', __('Image Editor Save Failed'));
             }
         }
     } else {
         return new WP_Error('image_save_error', __('Image Editor Save Failed'));
     }
     // Set correct file permissions
     $stat = stat(dirname($filename));
     $perms = $stat['mode'] & 0666;
     //same permissions as parent folder, strip off the executable bits
     @chmod($filename, $perms);
     ewww_image_optimizer_aux_images_loop($filename, true);
     $ewww_debug = "{$ewww_debug} image editor (gd) saved: {$filename} <br>";
     $image_size = filesize($filename);
     $ewww_debug = "{$ewww_debug} image editor size: {$image_size} <br>";
     ewww_image_optimizer_debug_log();
     return array('path' => $filename, 'file' => wp_basename(apply_filters('image_make_intermediate_size', $filename)), 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type);
 }
Example #14
0
function thumbnail($image, $width, $height, $extension)
{
    $data = getImageSize($image);
    $imageInfo["width"] = $data[0];
    $imageInfo["height"] = $data[1];
    $imageInfo["type"] = $data[2];
    switch ($imageInfo["type"]) {
        case 1:
            $img = imagecreatefromgif($image);
            break;
        case 2:
            $img = imageCreatefromjpeg($image);
            break;
        case 3:
            $img = imageCreatefrompng($image);
            break;
        default:
            return false;
    }
    $size["width"] = $imageInfo["width"];
    $size["height"] = $imageInfo["height"];
    if ($width < $imageInfo["width"]) {
        $size["width"] = $width;
    }
    if ($height < $imageInfo["height"]) {
        $size["height"] = $height;
    }
    if ($imageInfo["width"] * $size["width"] > $imageInfo["height"] * $size["height"]) {
        $size["height"] = round($imageInfo["height"] * $size["width"] / $imageInfo["width"]);
    } else {
        $size["width"] = round($imageInfo["width"] * $size["height"] / $imageInfo["height"]);
    }
    $newImg = imagecreatetruecolor($size["width"], $size["height"]);
    $otsc = imagecolortransparent($img);
    if ($otsc >= 0 && $otsc <= imagecolorstotal($img)) {
        $tran = imagecolorsforindex($img, $otsc);
        $newt = imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
        imagefill($newImg, 0, 0, $newt);
        imagecolortransparent($newImg, $newt);
    }
    imagecopyresized($newImg, $img, 0, 0, 0, 0, $size["width"], $size["height"], $imageInfo["width"], $imageInfo["height"]);
    imagedestroy($img);
    $newName = str_replace('.', $extension . '.', $image);
    switch ($imageInfo["type"]) {
        case 1:
            $result = imageGif($newImg, $newName);
            break;
        case 2:
            $result = imageJPEG($newImg, $newName);
            break;
        case 3:
            $result = imagepng($newImg, $newName);
            break;
    }
    imagedestroy($newImg);
}
Example #15
0
function invisible($Image) {
	$Count = imagecolorstotal($Image);
	if ($Count == 0) { return false; }
	$TotalAlpha = 0;
	for ($i=0; $i<$Count; ++$i) {
		$Color = imagecolorsforindex($Image,$i);
		$TotalAlpha += $Color['alpha'];
	}
	return (($TotalAlpha/$Count) == 127) ? true : false;
	
}
function AdjBrightContrast($img, $bright, $contr)
{
    $nbr = imagecolorstotal($img);
    for ($i = 0; $i < $nbr; ++$i) {
        $colarr = imagecolorsforindex($img, $i);
        $r = AdjRGBBrightContrast($colarr["red"], $bright, $contr);
        $g = AdjRGBBrightContrast($colarr["green"], $bright, $contr);
        $b = AdjRGBBrightContrast($colarr["blue"], $bright, $contr);
        imagecolorset($img, $i, $r, $g, $b);
    }
}
 /**
  * Workaround method for restoring alpha transparency for gif images
  *
  * @param resource  $src
  * @param resource  $dest
  * @return resource       transparent gf color
  */
 public static function restoreGifAlphaColor(&$src, &$dest)
 {
     $transparentcolor = imagecolortransparent($src);
     if ($transparentcolor != -1) {
         $colorcount = imagecolorstotal($src);
         imagetruecolortopalette($dest, true, $colorcount);
         imagepalettecopy($dest, $src);
         imagefill($dest, 0, 0, $transparentcolor);
         imagecolortransparent($dest, $transparentcolor);
     }
     return $transparentcolor;
 }
Example #18
0
 /**
  * @param  resource $resource
  * @param  resource $controlResource
  * @return resource
  */
 public function getOptimizedGdResource($resource, $controlResource)
 {
     if (imageistruecolor($resource)) {
         $resource = $this->rh->getPalettizedGdResource($resource);
     }
     $totalColors = imagecolorstotal($resource);
     $trans = imagecolortransparent($resource);
     $reds = new \SplFixedArray($totalColors);
     $greens = new \SplFixedArray($totalColors);
     $blues = new \SplFixedArray($totalColors);
     $i = 0;
     do {
         $colors = imagecolorsforindex($resource, $i);
         $reds[$i] = $colors['red'];
         $greens[$i] = $colors['green'];
         $blues[$i] = $colors['blue'];
     } while (++$i < $totalColors);
     if (imageistruecolor($controlResource)) {
         $controlResource = $this->rh->getPalettizedGdResource($controlResource);
     }
     $controlTotalColors = imagecolorstotal($controlResource);
     $controlTrans = imagecolortransparent($controlResource);
     $controlReds = new \SplFixedArray($controlTotalColors);
     $controlGreens = new \SplFixedArray($controlTotalColors);
     $controlBlues = new \SplFixedArray($controlTotalColors);
     $i = 0;
     do {
         $colors = imagecolorsforindex($controlResource, $i);
         $controlReds[$i] = $colors['red'];
         $controlGreens[$i] = $colors['green'];
         $controlBlues[$i] = $colors['blue'];
     } while (++$i < $controlTotalColors);
     $width = imagesx($resource);
     $height = imagesy($resource);
     $y = 0;
     do {
         $x = 0;
         do {
             $index = imagecolorat($resource, $x, $y);
             $red = $reds[$index];
             $green = $greens[$index];
             $blue = $blues[$index];
             $controlIndex = imagecolorat($controlResource, $x, $y);
             $controlRed = $controlReds[$controlIndex];
             $controlGreen = $controlGreens[$controlIndex];
             $controlBlue = $controlBlues[$controlIndex];
             if (($red & 0b11111100) === ($controlRed & 0b11111100) && ($green & 0b11111100) === ($controlGreen & 0b11111100) && ($blue & 0b11111100) === ($controlBlue & 0b11111100)) {
                 imagesetpixel($resource, $x, $y, $trans);
             }
         } while (++$x !== $width);
     } while (++$y !== $height);
     return $resource;
 }
Example #19
0
 public function toPngColor($img)
 {
     $color = imagecolorexact($img, $this->red, $this->green, $this->blue);
     if ($color == -1) {
         if (imagecolorstotal($img) >= 255) {
             $color = imagecolorclosest($img, $this->red, $this->green, $this->blue);
         } else {
             $color = imagecolorallocate($img, $this->red, $this->green, $this->blue);
         }
     }
     return $color;
 }
Example #20
0
 public function build()
 {
     $im = imagecreatefromgif($this->image_file);
     if ($im === false) {
         throw new Exception($this->image_file . ' is not gif file.');
     }
     $colortable_num = imagecolorstotal($im);
     $transparent_index = imagecolortransparent($im);
     $colortable = '';
     if ($transparent_index < 0) {
         for ($i = 0; $i < $colortable_num; ++$i) {
             $rgb = imagecolorsforindex($im, $i);
             $colortable .= chr($rgb['red']);
             $colortable .= chr($rgb['green']);
             $colortable .= chr($rgb['blue']);
         }
     } else {
         for ($i = 0; $i < $colortable_num; ++$i) {
             $rgb = imagecolorsforindex($im, $i);
             $colortable .= chr($rgb['red']);
             $colortable .= chr($rgb['green']);
             $colortable .= chr($rgb['blue']);
             $colortable .= $i == $transparent_index ? chr(0) : chr(255);
         }
     }
     $pixeldata = '';
     $i = 0;
     $width = imagesx($im);
     $height = imagesy($im);
     for ($y = 0; $y < $height; ++$y) {
         for ($x = 0; $x < $width; ++$x) {
             $pixeldata .= chr(imagecolorat($im, $x, $y));
             $i++;
         }
         while ($i % 4 != 0) {
             $pixeldata .= chr(0);
             $i++;
         }
     }
     $format = 3;
     // palette format
     $content = chr($format) . pack('v', $width) . pack('v', $height);
     $content .= chr($colortable_num - 1) . gzcompress($colortable . $pixeldata);
     if ($transparent_index < 0) {
         $this->code = 20;
         // DefineBitsLossless
     } else {
         $this->code = 36;
         // DefineBitsLossless2
     }
     $this->content = $content;
 }
Example #21
0
 public function dump($var)
 {
     $result = array();
     $gd_info = gd_info();
     $width = imagesx($var);
     $height = imagesy($var);
     $colors_palette = imagecolorstotal($var);
     $is_true_color = imageistruecolor($var) ? 'TRUE' : 'FALSE';
     ob_start();
     imagepng($var);
     $image = ob_get_clean();
     $gd_support = array();
     if ($gd_info['FreeType Support']) {
         $gd_support[] = 'FreeType(' . $gd_info['FreeType Linkage'] . ')';
     }
     if ($gd_info['T1Lib Support']) {
         $gd_support[] = 'T1Lib';
     }
     if ($gd_info['GIF Read Support'] || $gd_info['GIF Create Support']) {
         if ($gd_info['GIF Read Support'] && $gd_info['GIF Create Support']) {
             $gd_support[] = 'GIF';
         } elseif ($gd_info['GIF Read Support']) {
             $gd_support[] = 'GIF(read)';
         } elseif ($gd_info['GIF Create Support']) {
             $gd_support[] = 'GIF(create)';
         }
     }
     if ($gd_info['JPEG Support']) {
         $gd_support[] = 'JPEG';
     }
     if ($gd_info['PNG Support']) {
         $gd_support[] = 'PNG';
     }
     if ($gd_info['WBMP Support']) {
         $gd_support[] = 'WBMP';
     }
     if ($gd_info['XPM Support']) {
         $gd_support[] = 'XPM';
     }
     if ($gd_info['XBM Support']) {
         $gd_support[] = 'XBM';
     }
     if ($gd_info['JIS-mapped Japanese Font Support']) {
         $gd_support[] = 'JIS-mapped Japanese Font';
     }
     // gd info
     $result['GD'] = array('version' => $gd_info['GD Version'], 'support' => implode(', ', $gd_support));
     // image info
     $result['image'] = array('width' => $width . 'px', 'height' => $height . 'px', 'colors_palette' => $colors_palette, 'true_color' => $is_true_color, 'image' => 'data:image/png;base64,' . base64_encode($image));
     return $result;
 }
Example #22
0
function createThumbs($pathToImages, $pathToThumbs, $thumbWidth)
{
    $quality = 100;
    $info = getimagesize($pathToImages);
    // load image and get image size
    if ($info[2] == IMAGETYPE_JPEG) {
        $image = imagecreatefromjpeg("{$pathToImages}");
    } elseif ($info[2] == IMAGETYPE_GIF) {
        $image = imagecreatefromgif("{$pathToImages}");
    } elseif ($info[2] == IMAGETYPE_PNG) {
        $image = imagecreatefrompng("{$pathToImages}");
    }
    $width = imagesx($image);
    $height = imagesy($image);
    if ($width <= $thumbWidth) {
        copy($pathToImages, $pathToThumbs);
        return true;
    }
    // calculate thumbnail size
    $new_width = $thumbWidth;
    $new_height = floor($height * ($thumbWidth / $width));
    // create a new temporary image
    $image_resized = imagecreatetruecolor($new_width, $new_height);
    if ($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG) {
        $transparency = imagecolortransparent($image);
        $palletsize = imagecolorstotal($image);
        if ($transparency >= 0 && $transparency < $palletsize) {
            $transparent_color = imagecolorsforindex($image, $transparency);
            $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
            imagefill($image_resized, 0, 0, $transparency);
            imagecolortransparent($image_resized, $transparency);
        } elseif ($info[2] == IMAGETYPE_PNG) {
            imagealphablending($image_resized, false);
            $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
            imagefill($image_resized, 0, 0, $color);
            imagesavealpha($image_resized, true);
        }
    }
    // copy and resize old image into new image
    imagecopyresized($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // save thumbnail into a file
    if ($info[2] == IMAGETYPE_GIF) {
        imagegif($image_resized, "{$pathToThumbs}", $quality);
    } elseif ($info[2] == IMAGETYPE_JPEG) {
        imagejpeg($image_resized, "{$pathToThumbs}", $quality);
    } elseif ($info[2] == IMAGETYPE_PNG) {
        $quality = 9 - (int) (0.9 * $quality / 10.0);
        imagepng($image_resized, "{$pathToThumbs}", $quality);
    }
    return true;
}
Example #23
0
 private function kidOfImage($srcImg, $size, $imgInfo)
 {
     $newImg = imagecreatetruecolor($size["width"], $size["height"]);
     $otsc = imagecolortransparent($srcImg);
     if ($otsc >= 0 && $otsc <= imagecolorstotal($srcImg)) {
         $tran = imagecolorsforindex($srcImg, $otsc);
         $newt = imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
         imagefill($newImg, 0, 0, $newt);
         imagecolortransparent($newImg, $newt);
     }
     imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);
     imagedestroy($srcImg);
     return $newImg;
 }
 public static function run($res, $rotation = NULL, $background = NULL)
 {
     // replace transparent background with another color (imagefill or via imagecolortransparent)
     $color = self::html2rgb($background);
     $stubColor = imagecolorallocate($res, $color[0], $color[1], $color[2]);
     imagefill($res, 0, 0, $stubColor);
     $imgcolors = imagecolorstotal($res);
     // rotate the image anticlockwise
     $resRotated = imagerotate($res, $rotation, $stubColor);
     imagedestroy($res);
     // replace color with transparent
     imagetruecolortopalette($resRotated, false, $imgcolors);
     imagecolortransparent($resRotated, imagecolorat($resRotated, 0, 0));
     return $resRotated;
 }
Example #25
0
 protected final function _getColorAt($resource, $x, $y)
 {
     $color = imagecolorat($resource, (int) $x, (int) $y);
     if ($color === false) {
         throw new Exception("Cannot get color");
     }
     if (imagecolorstotal($resource)) {
         $rgba = imagecolorsforindex($resource, $color);
         if (!is_array($rgba)) {
             throw new Exception("Cannot get colors for specified index");
         }
         return array($rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
     } else {
         return array($color >> 16 & 0xff, $color >> 8 & 0xff, $color & 0xff, ($color & 0x7f000000) >> 24);
     }
 }
 /**
  * Insert a color to the palet or return the color if the color exist
  *
  * @param image $pic the picture where we work on it
  * @param int $c1 red part of the color
  * @param int $c2 green part of the color
  * @param int $c3 blue part of the color
  * @return color the color that we want
  */
 public static function createcolor($pic, $c1, $c2, $c3)
 {
     //get color from palette
     $color = imagecolorexact($pic, $c1, $c2, $c3);
     if ($color == -1) {
         //color does not exist...
         //test if we have used up palette
         if (imagecolorstotal($pic) >= 255) {
             //palette used up; pick closest assigned color
             $color = imagecolorclosest($pic, $c1, $c2, $c3);
         } else {
             //palette NOT used up; assign new color
             $color = imagecolorallocate($pic, $c1, $c2, $c3);
         }
     }
     return $color;
 }
Example #27
0
 public function execute()
 {
     $this->params['filter_r'] = (int) $this->params['filter_r'];
     if ($this->params['filter_r'] < 0) {
         return;
     }
     $this->params['filter_g'] = (int) $this->params['filter_g'];
     if ($this->params['filter_g'] < 0) {
         return;
     }
     $this->params['filter_b'] = (int) $this->params['filter_b'];
     if ($this->params['filter_b'] < 0) {
         return;
     }
     $this->media->asImage();
     $img = $this->media->getImage();
     if (!($t = imagecolorstotal($img))) {
         $t = 256;
         imagetruecolortopalette($img, true, $t);
     }
     $imagex = imagesx($img);
     $imagey = imagesy($img);
     $gdimage = $this->media->getImage();
     $w = $this->media->getWidth();
     $h = $this->media->getHeight();
     $src_x = ceil($w);
     $src_y = ceil($h);
     $dst_x = $src_x;
     $dst_y = $src_y;
     $dst_im = imagecreatetruecolor($dst_x, $dst_y);
     imagecopyresampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y);
     for ($y = 0; $y < $src_y; ++$y) {
         for ($x = 0; $x < $src_x; ++$x) {
             $rgb = imagecolorat($dst_im, $x, $y);
             $TabColors = imagecolorsforindex($dst_im, $rgb);
             $color_r = floor($TabColors['red'] * $this->params['filter_r'] / 255);
             $color_g = floor($TabColors['green'] * $this->params['filter_g'] / 255);
             $color_b = floor($TabColors['blue'] * $this->params['filter_b'] / 255);
             $newcol = imagecolorallocate($dst_im, $color_r, $color_g, $color_b);
             imagesetpixel($dst_im, $x, $y, $newcol);
         }
     }
     $this->media->setImage($dst_im);
 }
Example #28
0
 /**
  * Actually uploads the file, and act on it according to the set processing class variables
  *
  * This function copies the uploaded file to the given location, eventually performing actions on it.
  * Typically, you can call {@link process} several times for the same file,
  * for instance to create a resized image and a thumbnail of the same file.
  * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times.
  * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls.
  *
  * According to the processing class variables set in the calling file, the file can be renamed,
  * and if it is an image, can be resized or converted.
  *
  * When the processing is completed, and the file copied to its new location, the
  * processing class variables will be reset to their default value.
  * This allows you to set new properties, and perform another {@link process} on the same uploaded file
  *
  * If the function is called with a null or empty argument, then it will return the content of the picture
  *
  * It will set {@link processed} (and {@link error} is an error occurred)
  *
  * @access public
  * @param  string $server_path Optional path location of the uploaded file, with an ending slash
  * @return string Optional content of the image
  */
 function process($server_path = null)
 {
     $this->error = '';
     $this->processed = true;
     $return_mode = false;
     $return_content = null;
     // clean up dst variables
     $this->file_dst_path = '';
     $this->file_dst_pathname = '';
     $this->file_dst_name = '';
     $this->file_dst_name_body = '';
     $this->file_dst_name_ext = '';
     if (!$this->uploaded) {
         $this->error = $this->translate('file_not_uploaded');
         $this->processed = false;
     }
     if ($this->processed) {
         if (empty($server_path) || is_null($server_path)) {
             $this->log .= '<b>process file and return the content</b><br />';
             $return_mode = true;
         } else {
             if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
                 if (substr($server_path, -1, 1) != '\\') {
                     $server_path = $server_path . '\\';
                 }
             } else {
                 if (substr($server_path, -1, 1) != '/') {
                     $server_path = $server_path . '/';
                 }
             }
             $this->log .= '<b>process file to ' . $server_path . '</b><br />';
         }
     }
     if ($this->processed) {
         // checks file max size
         if ($this->file_src_size > $this->file_max_size) {
             $this->processed = false;
             $this->error = $this->translate('file_too_big');
         } else {
             $this->log .= '- file size OK<br />';
         }
     }
     if ($this->processed) {
         // if we have an image without extension, set it
         if ($this->file_is_image && !$this->file_src_name_ext_) {
             $this->file_src_name_ext = $this->file_src_name_ext_ = $this->image_src_type;
         }
         // turn dangerous scripts into text files
         if ($this->no_script) {
             if ((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name) || empty($this->file_src_name_ext_)) {
                 $this->file_src_mime = 'text/plain';
                 $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!<br />';
                 $this->file_src_name_ext = $this->file_src_name_ext_ . (empty($this->file_src_name_ext_) ? 'txt' : '.txt');
             }
         }
         if ($this->mime_check && empty($this->file_src_mime)) {
             $this->processed = false;
             $this->error = $this->translate('no_mime');
         } else {
             if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) {
                 list($m1, $m2) = explode('/', $this->file_src_mime);
                 $allowed = false;
                 // check wether the mime type is allowed
                 foreach ($this->allowed as $k => $v) {
                     list($v1, $v2) = explode('/', $v);
                     if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) {
                         $allowed = true;
                         break;
                     }
                 }
                 // check wether the mime type is forbidden
                 foreach ($this->forbidden as $k => $v) {
                     list($v1, $v2) = explode('/', $v);
                     if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) {
                         $allowed = false;
                         break;
                     }
                 }
                 if (!$allowed) {
                     $this->processed = false;
                     $this->error = $this->translate('incorrect_file');
                 } else {
                     $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />';
                 }
             } else {
                 $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />';
             }
         }
         // if the file is an image, we can check on its dimensions
         // these checks are not available if open_basedir restrictions are in place
         if ($this->file_is_image) {
             if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) {
                 $ratio = $this->image_src_x / $this->image_src_y;
                 if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) {
                     $this->processed = false;
                     $this->error = $this->translate('image_too_wide');
                 }
                 if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) {
                     $this->processed = false;
                     $this->error = $this->translate('image_too_narrow');
                 }
                 if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) {
                     $this->processed = false;
                     $this->error = $this->translate('image_too_high');
                 }
                 if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) {
                     $this->processed = false;
                     $this->error = $this->translate('image_too_short');
                 }
                 if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) {
                     $this->processed = false;
                     $this->error = $this->translate('ratio_too_high');
                 }
                 if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) {
                     $this->processed = false;
                     $this->error = $this->translate('ratio_too_low');
                 }
                 if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) {
                     $this->processed = false;
                     $this->error = $this->translate('too_many_pixels');
                 }
                 if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) {
                     $this->processed = false;
                     $this->error = $this->translate('not_enough_pixels');
                 }
             } else {
                 $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />';
             }
         }
     }
     if ($this->processed) {
         $this->file_dst_path = $server_path;
         // repopulate dst variables from src
         $this->file_dst_name = $this->file_src_name;
         $this->file_dst_name_body = $this->file_src_name_body;
         $this->file_dst_name_ext = $this->file_src_name_ext;
         if ($this->file_overwrite) {
             $this->file_auto_rename = false;
         }
         if ($this->image_convert != '') {
             // if we convert as an image
             $this->file_dst_name_ext = $this->image_convert;
             $this->log .= '- new file name ext : ' . $this->image_convert . '<br />';
         }
         if ($this->file_new_name_body != '') {
             // rename file body
             $this->file_dst_name_body = $this->file_new_name_body;
             $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />';
         }
         if ($this->file_new_name_ext != '') {
             // rename file ext
             $this->file_dst_name_ext = $this->file_new_name_ext;
             $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />';
         }
         if ($this->file_name_body_add != '') {
             // append a string to the name
             $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add;
             $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />';
         }
         if ($this->file_name_body_pre != '') {
             // prepend a string to the name
             $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body;
             $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />';
         }
         if ($this->file_safe_name) {
             // formats the name
             $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body);
             $this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);
             $this->log .= '- file name safe format<br />';
         }
         $this->log .= '- destination variables<br />';
         if (empty($this->file_dst_path) || is_null($this->file_dst_path)) {
             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path         : n/a<br />';
         } else {
             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path         : ' . $this->file_dst_path . '<br />';
         }
         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_body    : ' . $this->file_dst_name_body . '<br />';
         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_ext     : ' . $this->file_dst_name_ext . '<br />';
         // do we do some image manipulation?
         $image_manipulation = $this->file_is_image && ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || $this->image_unsharp || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_precrop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height);
         if ($image_manipulation) {
             if ($this->image_convert == '') {
                 $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
                 $this->log .= '- image operation, keep extension<br />';
             } else {
                 $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert;
                 $this->log .= '- image operation, change extension for conversion type<br />';
             }
         } else {
             $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
             $this->log .= '- no image operation, keep extension<br />';
         }
         if (!$return_mode) {
             if (!$this->file_auto_rename) {
                 $this->log .= '- no auto_rename if same filename exists<br />';
                 $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
             } else {
                 $this->log .= '- checking for auto_rename<br />';
                 $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
                 $body = $this->file_dst_name_body;
                 $cpt = 1;
                 while (@file_exists($this->file_dst_pathname)) {
                     $this->file_dst_name_body = $body . '_' . $cpt;
                     $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
                     $cpt++;
                     $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
                 }
                 if ($cpt > 1) {
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' . $this->file_dst_name . '<br />';
                 }
             }
             $this->log .= '- destination file details<br />';
             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name         : ' . $this->file_dst_name . '<br />';
             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname     : ' . $this->file_dst_pathname . '<br />';
             if ($this->file_overwrite) {
                 $this->log .= '- no overwrite checking<br />';
             } else {
                 if (@file_exists($this->file_dst_pathname)) {
                     $this->processed = false;
                     $this->error = $this->translate('already_exists', array($this->file_dst_name));
                 } else {
                     $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />';
                 }
             }
         }
     }
     if ($this->processed) {
         // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
         if (!empty($this->file_src_temp)) {
             $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
             $this->file_src_pathname = $this->file_src_temp;
             if (!file_exists($this->file_src_pathname)) {
                 $this->processed = false;
                 $this->error = $this->translate('temp_file_missing');
             }
             // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
         } else {
             if (!$this->no_upload_check) {
                 if (!is_uploaded_file($this->file_src_pathname)) {
                     $this->processed = false;
                     $this->error = $this->translate('source_missing');
                 }
                 // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
             } else {
                 if (!file_exists($this->file_src_pathname)) {
                     $this->processed = false;
                     $this->error = $this->translate('source_missing');
                 }
             }
         }
         // checks if the destination directory exists, and attempt to create it
         if (!$return_mode) {
             if ($this->processed && !file_exists($this->file_dst_path)) {
                 if ($this->dir_auto_create) {
                     $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:';
                     if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
                         $this->log .= ' failed<br />';
                         $this->processed = false;
                         $this->error = $this->translate('destination_dir');
                     } else {
                         $this->log .= ' success<br />';
                     }
                 } else {
                     $this->error = $this->translate('destination_dir_missing');
                 }
             }
             if ($this->processed && !is_dir($this->file_dst_path)) {
                 $this->processed = false;
                 $this->error = $this->translate('destination_path_not_dir');
             }
             // checks if the destination directory is writeable, and attempt to make it writeable
             $hash = md5($this->file_dst_name_body . rand(1, 1000));
             if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) {
                 if ($this->dir_auto_chmod) {
                     $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:';
                     if (!@chmod($this->file_dst_path, $this->dir_chmod)) {
                         $this->log .= ' failed<br />';
                         $this->processed = false;
                         $this->error = $this->translate('destination_dir_write');
                     } else {
                         $this->log .= ' success<br />';
                         if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) {
                             // we re-check
                             $this->processed = false;
                             $this->error = $this->translate('destination_dir_write');
                         } else {
                             @fclose($f);
                         }
                     }
                 } else {
                     $this->processed = false;
                     $this->error = $this->translate('destination_path_write');
                 }
             } else {
                 if ($this->processed) {
                     @fclose($f);
                 }
                 @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext);
             }
             // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction)
             // then we create a temp file that will be used as the source file in subsequent processes
             // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists)
             if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) {
                 $this->log .= '- attempting to use a temp file:';
                 $hash = md5($this->file_dst_name_body . rand(1, 1000));
                 if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) {
                     $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext;
                     $this->file_src_temp = $this->file_src_pathname;
                     $this->log .= ' file created<br />';
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' . $this->file_src_temp . '<br />';
                 } else {
                     $this->log .= ' failed<br />';
                     $this->processed = false;
                     $this->error = $this->translate('temp_file');
                 }
             }
         }
     }
     if ($this->processed) {
         // we do a quick check to ensure the file is really an image
         // we can do this only now, as it would have failed before in case of open_basedir
         if ($image_manipulation && !@getimagesize($this->file_src_pathname)) {
             $this->log .= '- the file is not an image!<br />';
             $image_manipulation = false;
         }
         if ($image_manipulation) {
             // checks if the source file is readable
             if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) {
                 $this->processed = false;
                 $this->error = $this->translate('source_not_readable');
             } else {
                 @fclose($f);
             }
             // we now do all the image manipulations
             $this->log .= '- image resizing or conversion wanted<br />';
             if ($this->gdversion()) {
                 switch ($this->image_src_type) {
                     case 'jpg':
                         if (!function_exists('imagecreatefromjpeg')) {
                             $this->processed = false;
                             $this->error = $this->translate('no_create_support', array('JPEG'));
                         } else {
                             $image_src = @imagecreatefromjpeg($this->file_src_pathname);
                             if (!$image_src) {
                                 $this->processed = false;
                                 $this->error = $this->translate('create_error', array('JPEG'));
                             } else {
                                 $this->log .= '- source image is JPEG<br />';
                             }
                         }
                         break;
                     case 'png':
                         if (!function_exists('imagecreatefrompng')) {
                             $this->processed = false;
                             $this->error = $this->translate('no_create_support', array('PNG'));
                         } else {
                             $image_src = @imagecreatefrompng($this->file_src_pathname);
                             if (!$image_src) {
                                 $this->processed = false;
                                 $this->error = $this->translate('create_error', array('PNG'));
                             } else {
                                 $this->log .= '- source image is PNG<br />';
                             }
                         }
                         break;
                     case 'gif':
                         if (!function_exists('imagecreatefromgif')) {
                             $this->processed = false;
                             $this->error = $this->translate('no_create_support', array('GIF'));
                         } else {
                             $image_src = @imagecreatefromgif($this->file_src_pathname);
                             if (!$image_src) {
                                 $this->processed = false;
                                 $this->error = $this->translate('create_error', array('GIF'));
                             } else {
                                 $this->log .= '- source image is GIF<br />';
                             }
                         }
                         break;
                     case 'bmp':
                         if (!method_exists($this, 'imagecreatefrombmp')) {
                             $this->processed = false;
                             $this->error = $this->translate('no_create_support', array('BMP'));
                         } else {
                             $image_src = @$this->imagecreatefrombmp($this->file_src_pathname);
                             if (!$image_src) {
                                 $this->processed = false;
                                 $this->error = $this->translate('create_error', array('BMP'));
                             } else {
                                 $this->log .= '- source image is BMP<br />';
                             }
                         }
                         break;
                     default:
                         $this->processed = false;
                         $this->error = $this->translate('source_invalid');
                 }
             } else {
                 $this->processed = false;
                 $this->error = $this->translate('gd_missing');
             }
             if ($this->processed && $image_src) {
                 // we have to set image_convert if it is not already
                 if (empty($this->image_convert)) {
                     $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '<br />';
                     $this->image_convert = $this->file_src_name_ext;
                 }
                 if (!in_array($this->image_convert, $this->image_supported)) {
                     $this->image_convert = 'jpg';
                 }
                 // we set the default color to be the background color if we don't output in a transparent format
                 if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) {
                     $this->image_background_color = $this->image_default_color;
                 }
                 if (!empty($this->image_background_color)) {
                     $this->image_default_color = $this->image_background_color;
                 }
                 if (empty($this->image_default_color)) {
                     $this->image_default_color = '#FFFFFF';
                 }
                 $this->image_src_x = imagesx($image_src);
                 $this->image_src_y = imagesy($image_src);
                 $gd_version = $this->gdversion();
                 $ratio_crop = null;
                 if (!imageistruecolor($image_src)) {
                     // $this->image_src_type == 'gif'
                     $this->log .= '- image is detected as having a palette<br />';
                     $this->image_is_palette = true;
                     $this->image_transparent_color = imagecolortransparent($image_src);
                     if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) {
                         $this->image_is_transparent = true;
                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;palette image is detected as transparent<br />';
                     }
                     // if the image has a palette (GIF), we convert it to true color, preserving transparency
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;convert palette image to true color<br />';
                     $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y);
                     imagealphablending($true_color, false);
                     imagesavealpha($true_color, true);
                     for ($x = 0; $x < $this->image_src_x; $x++) {
                         for ($y = 0; $y < $this->image_src_y; $y++) {
                             if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) {
                                 imagesetpixel($true_color, $x, $y, 127 << 24);
                             } else {
                                 $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y));
                                 imagesetpixel($true_color, $x, $y, $rgb['alpha'] << 24 | $rgb['red'] << 16 | $rgb['green'] << 8 | $rgb['blue']);
                             }
                         }
                     }
                     $image_src = $this->imagetransfer($true_color, $image_src);
                     imagealphablending($image_src, false);
                     imagesavealpha($image_src, true);
                     $this->image_is_palette = false;
                 }
                 $image_dst =& $image_src;
                 // pre-crop image, before resizing
                 if (!empty($this->image_precrop)) {
                     if (is_array($this->image_precrop)) {
                         $vars = $this->image_precrop;
                     } else {
                         $vars = explode(' ', $this->image_precrop);
                     }
                     if (sizeof($vars) == 4) {
                         $ct = $vars[0];
                         $cr = $vars[1];
                         $cb = $vars[2];
                         $cl = $vars[3];
                     } else {
                         if (sizeof($vars) == 2) {
                             $ct = $vars[0];
                             $cr = $vars[1];
                             $cb = $vars[0];
                             $cl = $vars[1];
                         } else {
                             $ct = $vars[0];
                             $cr = $vars[0];
                             $cb = $vars[0];
                             $cl = $vars[0];
                         }
                     }
                     if (strpos($ct, '%') > 0) {
                         $ct = $this->image_src_y * (str_replace('%', '', $ct) / 100);
                     }
                     if (strpos($cr, '%') > 0) {
                         $cr = $this->image_src_x * (str_replace('%', '', $cr) / 100);
                     }
                     if (strpos($cb, '%') > 0) {
                         $cb = $this->image_src_y * (str_replace('%', '', $cb) / 100);
                     }
                     if (strpos($cl, '%') > 0) {
                         $cl = $this->image_src_x * (str_replace('%', '', $cl) / 100);
                     }
                     if (strpos($ct, 'px') > 0) {
                         $ct = str_replace('px', '', $ct);
                     }
                     if (strpos($cr, 'px') > 0) {
                         $cr = str_replace('px', '', $cr);
                     }
                     if (strpos($cb, 'px') > 0) {
                         $cb = str_replace('px', '', $cb);
                     }
                     if (strpos($cl, 'px') > 0) {
                         $cl = str_replace('px', '', $cl);
                     }
                     $ct = (int) $ct;
                     $cr = (int) $cr;
                     $cb = (int) $cb;
                     $cl = (int) $cl;
                     $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
                     $this->image_src_x = $this->image_src_x - $cl - $cr;
                     $this->image_src_y = $this->image_src_y - $ct - $cb;
                     if ($this->image_src_x < 1) {
                         $this->image_src_x = 1;
                     }
                     if ($this->image_src_y < 1) {
                         $this->image_src_y = 1;
                     }
                     $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y);
                     // we copy the image into the recieving image
                     imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y);
                     // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
                     if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) {
                         // use the background color if present
                         if (!empty($this->image_background_color)) {
                             list($red, $green, $blue) = $this->getcolors($this->image_background_color);
                             $fill = imagecolorallocate($tmp, $red, $green, $blue);
                         } else {
                             $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
                         }
                         // fills eventual negative margins
                         if ($ct < 0) {
                             imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill);
                         }
                         if ($cr < 0) {
                             imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill);
                         }
                         if ($cb < 0) {
                             imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill);
                         }
                         if ($cl < 0) {
                             imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill);
                         }
                     }
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
                 if ($this->image_resize) {
                     $this->log .= '- resizing...<br />';
                     if ($this->image_ratio_x) {
                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x size<br />';
                         $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y);
                         $this->image_dst_y = $this->image_y;
                     } else {
                         if ($this->image_ratio_y) {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate y size<br />';
                             $this->image_dst_x = $this->image_x;
                             $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x);
                         } else {
                             if (is_numeric($this->image_ratio_pixels)) {
                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x/y size to match a number of pixels<br />';
                                 $pixels = $this->image_src_y * $this->image_src_x;
                                 $diff = sqrt($this->image_ratio_pixels / $pixels);
                                 $this->image_dst_x = round($this->image_src_x * $diff);
                                 $this->image_dst_y = round($this->image_src_y * $diff);
                             } else {
                                 if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) {
                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;check x/y sizes<br />';
                                     if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) {
                                         $this->image_dst_x = $this->image_x;
                                         $this->image_dst_y = $this->image_y;
                                         if ($this->image_ratio_crop) {
                                             if (!is_string($this->image_ratio_crop)) {
                                                 $this->image_ratio_crop = '';
                                             }
                                             $this->image_ratio_crop = strtolower($this->image_ratio_crop);
                                             if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) {
                                                 $this->image_dst_y = $this->image_y;
                                                 $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y));
                                                 $ratio_crop = array();
                                                 $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
                                                 if (strpos($this->image_ratio_crop, 'l') !== false) {
                                                     $ratio_crop['l'] = 0;
                                                     $ratio_crop['r'] = $ratio_crop['x'];
                                                 } else {
                                                     if (strpos($this->image_ratio_crop, 'r') !== false) {
                                                         $ratio_crop['l'] = $ratio_crop['x'];
                                                         $ratio_crop['r'] = 0;
                                                     } else {
                                                         $ratio_crop['l'] = round($ratio_crop['x'] / 2);
                                                         $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
                                                     }
                                                 }
                                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_x         : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
                                                 if (is_null($this->image_crop)) {
                                                     $this->image_crop = array(0, 0, 0, 0);
                                                 }
                                             } else {
                                                 $this->image_dst_x = $this->image_x;
                                                 $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x));
                                                 $ratio_crop = array();
                                                 $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
                                                 if (strpos($this->image_ratio_crop, 't') !== false) {
                                                     $ratio_crop['t'] = 0;
                                                     $ratio_crop['b'] = $ratio_crop['y'];
                                                 } else {
                                                     if (strpos($this->image_ratio_crop, 'b') !== false) {
                                                         $ratio_crop['t'] = $ratio_crop['y'];
                                                         $ratio_crop['b'] = 0;
                                                     } else {
                                                         $ratio_crop['t'] = round($ratio_crop['y'] / 2);
                                                         $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
                                                     }
                                                 }
                                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_y         : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
                                                 if (is_null($this->image_crop)) {
                                                     $this->image_crop = array(0, 0, 0, 0);
                                                 }
                                             }
                                         } else {
                                             if ($this->image_ratio_fill) {
                                                 if (!is_string($this->image_ratio_fill)) {
                                                     $this->image_ratio_fill = '';
                                                 }
                                                 $this->image_ratio_fill = strtolower($this->image_ratio_fill);
                                                 if ($this->image_src_x / $this->image_x < $this->image_src_y / $this->image_y) {
                                                     $this->image_dst_y = $this->image_y;
                                                     $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y));
                                                     $ratio_crop = array();
                                                     $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
                                                     if (strpos($this->image_ratio_fill, 'l') !== false) {
                                                         $ratio_crop['l'] = 0;
                                                         $ratio_crop['r'] = $ratio_crop['x'];
                                                     } else {
                                                         if (strpos($this->image_ratio_fill, 'r') !== false) {
                                                             $ratio_crop['l'] = $ratio_crop['x'];
                                                             $ratio_crop['r'] = 0;
                                                         } else {
                                                             $ratio_crop['l'] = round($ratio_crop['x'] / 2);
                                                             $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
                                                         }
                                                     }
                                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_x         : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
                                                     if (is_null($this->image_crop)) {
                                                         $this->image_crop = array(0, 0, 0, 0);
                                                     }
                                                 } else {
                                                     $this->image_dst_x = $this->image_x;
                                                     $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x));
                                                     $ratio_crop = array();
                                                     $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
                                                     if (strpos($this->image_ratio_fill, 't') !== false) {
                                                         $ratio_crop['t'] = 0;
                                                         $ratio_crop['b'] = $ratio_crop['y'];
                                                     } else {
                                                         if (strpos($this->image_ratio_fill, 'b') !== false) {
                                                             $ratio_crop['t'] = $ratio_crop['y'];
                                                             $ratio_crop['b'] = 0;
                                                         } else {
                                                             $ratio_crop['t'] = round($ratio_crop['y'] / 2);
                                                             $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
                                                         }
                                                     }
                                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_y         : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
                                                     if (is_null($this->image_crop)) {
                                                         $this->image_crop = array(0, 0, 0, 0);
                                                     }
                                                 }
                                             } else {
                                                 if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) {
                                                     $this->image_dst_x = $this->image_x;
                                                     $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x));
                                                 } else {
                                                     $this->image_dst_y = $this->image_y;
                                                     $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y));
                                                 }
                                             }
                                         }
                                     } else {
                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;doesn\'t calculate x/y sizes<br />';
                                         $this->image_dst_x = $this->image_src_x;
                                         $this->image_dst_y = $this->image_src_y;
                                     }
                                 } else {
                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;use plain sizes<br />';
                                     $this->image_dst_x = $this->image_x;
                                     $this->image_dst_y = $this->image_y;
                                 }
                             }
                         }
                     }
                     if ($this->image_dst_x < 1) {
                         $this->image_dst_x = 1;
                     }
                     if ($this->image_dst_y < 1) {
                         $this->image_dst_y = 1;
                     }
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     if ($gd_version >= 2) {
                         $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
                     } else {
                         $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
                     }
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;resized image object created<br />';
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x y        : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />';
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_dst_x y        : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />';
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 } else {
                     $this->image_dst_x = $this->image_src_x;
                     $this->image_dst_y = $this->image_src_y;
                 }
                 // crop image (and also crops if image_ratio_crop is used)
                 if (!empty($this->image_crop) || !is_null($ratio_crop)) {
                     if (is_array($this->image_crop)) {
                         $vars = $this->image_crop;
                     } else {
                         $vars = explode(' ', $this->image_crop);
                     }
                     if (sizeof($vars) == 4) {
                         $ct = $vars[0];
                         $cr = $vars[1];
                         $cb = $vars[2];
                         $cl = $vars[3];
                     } else {
                         if (sizeof($vars) == 2) {
                             $ct = $vars[0];
                             $cr = $vars[1];
                             $cb = $vars[0];
                             $cl = $vars[1];
                         } else {
                             $ct = $vars[0];
                             $cr = $vars[0];
                             $cb = $vars[0];
                             $cl = $vars[0];
                         }
                     }
                     if (strpos($ct, '%') > 0) {
                         $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100);
                     }
                     if (strpos($cr, '%') > 0) {
                         $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100);
                     }
                     if (strpos($cb, '%') > 0) {
                         $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100);
                     }
                     if (strpos($cl, '%') > 0) {
                         $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100);
                     }
                     if (strpos($ct, 'px') > 0) {
                         $ct = str_replace('px', '', $ct);
                     }
                     if (strpos($cr, 'px') > 0) {
                         $cr = str_replace('px', '', $cr);
                     }
                     if (strpos($cb, 'px') > 0) {
                         $cb = str_replace('px', '', $cb);
                     }
                     if (strpos($cl, 'px') > 0) {
                         $cl = str_replace('px', '', $cl);
                     }
                     $ct = (int) $ct;
                     $cr = (int) $cr;
                     $cb = (int) $cb;
                     $cl = (int) $cl;
                     // we adjust the cropping if we use image_ratio_crop
                     if (!is_null($ratio_crop)) {
                         if (array_key_exists('t', $ratio_crop)) {
                             $ct += $ratio_crop['t'];
                         }
                         if (array_key_exists('r', $ratio_crop)) {
                             $cr += $ratio_crop['r'];
                         }
                         if (array_key_exists('b', $ratio_crop)) {
                             $cb += $ratio_crop['b'];
                         }
                         if (array_key_exists('l', $ratio_crop)) {
                             $cl += $ratio_crop['l'];
                         }
                     }
                     $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
                     $this->image_dst_x = $this->image_dst_x - $cl - $cr;
                     $this->image_dst_y = $this->image_dst_y - $ct - $cb;
                     if ($this->image_dst_x < 1) {
                         $this->image_dst_x = 1;
                     }
                     if ($this->image_dst_y < 1) {
                         $this->image_dst_y = 1;
                     }
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     // we copy the image into the recieving image
                     imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
                     // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
                     if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) {
                         // use the background color if present
                         if (!empty($this->image_background_color)) {
                             list($red, $green, $blue) = $this->getcolors($this->image_background_color);
                             $fill = imagecolorallocate($tmp, $red, $green, $blue);
                         } else {
                             $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
                         }
                         // fills eventual negative margins
                         if ($ct < 0) {
                             imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct - 1, $fill);
                         }
                         if ($cr < 0) {
                             imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
                         }
                         if ($cb < 0) {
                             imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill);
                         }
                         if ($cl < 0) {
                             imagefilledrectangle($tmp, 0, 0, -$cl - 1, $this->image_dst_y, $fill);
                         }
                     }
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // flip image
                 if ($gd_version >= 2 && !empty($this->image_flip)) {
                     $this->image_flip = strtolower($this->image_flip);
                     $this->log .= '- flip image : ' . $this->image_flip . '<br />';
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     for ($x = 0; $x < $this->image_dst_x; $x++) {
                         for ($y = 0; $y < $this->image_dst_y; $y++) {
                             if (strpos($this->image_flip, 'v') !== false) {
                                 imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1);
                             } else {
                                 imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1);
                             }
                         }
                     }
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // rotate image
                 if ($gd_version >= 2 && is_numeric($this->image_rotate)) {
                     if (!in_array($this->image_rotate, array(0, 90, 180, 270))) {
                         $this->image_rotate = 0;
                     }
                     if ($this->image_rotate != 0) {
                         if ($this->image_rotate == 90 || $this->image_rotate == 270) {
                             $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x);
                         } else {
                             $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                         }
                         $this->log .= '- rotate image : ' . $this->image_rotate . '<br />';
                         for ($x = 0; $x < $this->image_dst_x; $x++) {
                             for ($y = 0; $y < $this->image_dst_y; $y++) {
                                 if ($this->image_rotate == 90) {
                                     imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1);
                                 } else {
                                     if ($this->image_rotate == 180) {
                                         imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1);
                                     } else {
                                         if ($this->image_rotate == 270) {
                                             imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1);
                                         } else {
                                             imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1);
                                         }
                                     }
                                 }
                             }
                         }
                         if ($this->image_rotate == 90 || $this->image_rotate == 270) {
                             $t = $this->image_dst_y;
                             $this->image_dst_y = $this->image_dst_x;
                             $this->image_dst_x = $t;
                         }
                         // we transfert tmp into image_dst
                         $image_dst = $this->imagetransfer($tmp, $image_dst);
                     }
                 }
                 // unsharp mask
                 if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) {
                     // Unsharp Mask for PHP - version 2.1.1
                     // Unsharp mask algorithm by Torstein Hønsi 2003-07.
                     // Used with permission
                     // Modified to support alpha transparency
                     if ($this->image_unsharp_amount > 500) {
                         $this->image_unsharp_amount = 500;
                     }
                     $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016;
                     if ($this->image_unsharp_radius > 50) {
                         $this->image_unsharp_radius = 50;
                     }
                     $this->image_unsharp_radius = $this->image_unsharp_radius * 2;
                     if ($this->image_unsharp_threshold > 255) {
                         $this->image_unsharp_threshold = 255;
                     }
                     $this->image_unsharp_radius = abs(round($this->image_unsharp_radius));
                     if ($this->image_unsharp_radius != 0) {
                         $this->image_dst_x = imagesx($image_dst);
                         $this->image_dst_y = imagesy($image_dst);
                         $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
                         $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
                         if (function_exists('imageconvolution')) {
                             // PHP >= 5.1
                             $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1));
                             imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
                             imageconvolution($blur, $matrix, 16, 0);
                         } else {
                             for ($i = 0; $i < $this->image_unsharp_radius; $i++) {
                                 imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y);
                                 // left
                                 $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50);
                                 // right
                                 $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50);
                                 // center
                                 imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
                                 $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333);
                                 // up
                                 $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25);
                                 // down
                             }
                         }
                         $p_new = array();
                         if ($this->image_unsharp_threshold > 0) {
                             for ($x = 0; $x < $this->image_dst_x - 1; $x++) {
                                 for ($y = 0; $y < $this->image_dst_y; $y++) {
                                     $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                     $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
                                     $p_new['red'] = abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'])) : $p_orig['red'];
                                     $p_new['green'] = abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'])) : $p_orig['green'];
                                     $p_new['blue'] = abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'])) : $p_orig['blue'];
                                     if ($p_orig['red'] != $p_new['red'] || $p_orig['green'] != $p_new['green'] || $p_orig['blue'] != $p_new['blue']) {
                                         $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
                                         imagesetpixel($image_dst, $x, $y, $color);
                                     }
                                 }
                             }
                         } else {
                             for ($x = 0; $x < $this->image_dst_x; $x++) {
                                 for ($y = 0; $y < $this->image_dst_y; $y++) {
                                     $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                     $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
                                     $p_new['red'] = $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'];
                                     if ($p_new['red'] > 255) {
                                         $p_new['red'] = 255;
                                     } elseif ($p_new['red'] < 0) {
                                         $p_new['red'] = 0;
                                     }
                                     $p_new['green'] = $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'];
                                     if ($p_new['green'] > 255) {
                                         $p_new['green'] = 255;
                                     } elseif ($p_new['green'] < 0) {
                                         $p_new['green'] = 0;
                                     }
                                     $p_new['blue'] = $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'];
                                     if ($p_new['blue'] > 255) {
                                         $p_new['blue'] = 255;
                                     } elseif ($p_new['blue'] < 0) {
                                         $p_new['blue'] = 0;
                                     }
                                     $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
                                     imagesetpixel($image_dst, $x, $y, $color);
                                 }
                             }
                         }
                         imagedestroy($canvas);
                         imagedestroy($blur);
                     }
                 }
                 // add color overlay
                 if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && $this->image_overlay_percent > 0 && !empty($this->image_overlay_color))) {
                     $this->log .= '- apply color overlay<br />';
                     list($red, $green, $blue) = $this->getcolors($this->image_overlay_color);
                     $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
                     $color = imagecolorallocate($filter, $red, $green, $blue);
                     imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color);
                     $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent);
                     imagedestroy($filter);
                 }
                 // add brightness, contrast and tint, turns to greyscale and inverts colors
                 if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) {
                     $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
                     if (!empty($this->image_tint_color)) {
                         list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color);
                     }
                     imagealphablending($image_dst, true);
                     for ($y = 0; $y < $this->image_dst_y; $y++) {
                         for ($x = 0; $x < $this->image_dst_x; $x++) {
                             if ($this->image_greyscale) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']);
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                             if (is_numeric($this->image_threshold)) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127;
                                 $r = $g = $b = $c > $this->image_threshold ? 255 : 0;
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                             if (is_numeric($this->image_brightness)) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0);
                                 $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0);
                                 $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0);
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                             if (is_numeric($this->image_contrast)) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0);
                                 $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0);
                                 $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0);
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                             if (!empty($this->image_tint_color)) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $r = min(round($tint_red * $pixel['red'] / 169), 255);
                                 $g = min(round($tint_green * $pixel['green'] / 169), 255);
                                 $b = min(round($tint_blue * $pixel['blue'] / 169), 255);
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                             if (!empty($this->image_negative)) {
                                 $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 $r = round(255 - $pixel['red']);
                                 $g = round(255 - $pixel['green']);
                                 $b = round(255 - $pixel['blue']);
                                 $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
                                 imagesetpixel($image_dst, $x, $y, $color);
                             }
                         }
                     }
                 }
                 // adds a border
                 if ($gd_version >= 2 && !empty($this->image_border)) {
                     if (is_array($this->image_border)) {
                         $vars = $this->image_border;
                         $this->log .= '- add border : ' . implode(' ', $this->image_border) . '<br />';
                     } else {
                         $this->log .= '- add border : ' . $this->image_border . '<br />';
                         $vars = explode(' ', $this->image_border);
                     }
                     if (sizeof($vars) == 4) {
                         $ct = $vars[0];
                         $cr = $vars[1];
                         $cb = $vars[2];
                         $cl = $vars[3];
                     } else {
                         if (sizeof($vars) == 2) {
                             $ct = $vars[0];
                             $cr = $vars[1];
                             $cb = $vars[0];
                             $cl = $vars[1];
                         } else {
                             $ct = $vars[0];
                             $cr = $vars[0];
                             $cb = $vars[0];
                             $cl = $vars[0];
                         }
                     }
                     if (strpos($ct, '%') > 0) {
                         $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100);
                     }
                     if (strpos($cr, '%') > 0) {
                         $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100);
                     }
                     if (strpos($cb, '%') > 0) {
                         $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100);
                     }
                     if (strpos($cl, '%') > 0) {
                         $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100);
                     }
                     if (strpos($ct, 'px') > 0) {
                         $ct = str_replace('px', '', $ct);
                     }
                     if (strpos($cr, 'px') > 0) {
                         $cr = str_replace('px', '', $cr);
                     }
                     if (strpos($cb, 'px') > 0) {
                         $cb = str_replace('px', '', $cb);
                     }
                     if (strpos($cl, 'px') > 0) {
                         $cl = str_replace('px', '', $cl);
                     }
                     $ct = (int) $ct;
                     $cr = (int) $cr;
                     $cb = (int) $cb;
                     $cl = (int) $cl;
                     $this->image_dst_x = $this->image_dst_x + $cl + $cr;
                     $this->image_dst_y = $this->image_dst_y + $ct + $cb;
                     if (!empty($this->image_border_color)) {
                         list($red, $green, $blue) = $this->getcolors($this->image_border_color);
                     }
                     // we now create an image, that we fill with the border color
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0);
                     imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background);
                     // we then copy the source image into the new image, without merging so that only the border is actually kept
                     imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // add frame border
                 if (is_numeric($this->image_frame)) {
                     if (is_array($this->image_frame_colors)) {
                         $vars = $this->image_frame_colors;
                         $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />';
                     } else {
                         $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />';
                         $vars = explode(' ', $this->image_frame_colors);
                     }
                     $nb = sizeof($vars);
                     $this->image_dst_x = $this->image_dst_x + $nb * 2;
                     $this->image_dst_y = $this->image_dst_y + $nb * 2;
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2);
                     for ($i = 0; $i < $nb; $i++) {
                         list($red, $green, $blue) = $this->getcolors($vars[$i]);
                         $c = imagecolorallocate($tmp, $red, $green, $blue);
                         if ($this->image_frame == 1) {
                             imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c);
                             imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c);
                             imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c);
                             imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c);
                         } else {
                             imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c);
                             imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c);
                             imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c);
                             imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c);
                         }
                     }
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // add bevel border
                 if ($this->image_bevel > 0) {
                     if (empty($this->image_bevel_color1)) {
                         $this->image_bevel_color1 = '#FFFFFF';
                     }
                     if (empty($this->image_bevel_color2)) {
                         $this->image_bevel_color2 = '#000000';
                     }
                     list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1);
                     list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2);
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
                     imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
                     imagealphablending($tmp, true);
                     for ($i = 0; $i < $this->image_bevel; $i++) {
                         $alpha = round($i / $this->image_bevel * 127);
                         $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha);
                         $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha);
                         imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c1);
                         imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2);
                         imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2);
                         imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c1);
                     }
                     // we transfert tmp into image_dst
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // add watermark image
                 if ($this->image_watermark != '' && file_exists($this->image_watermark)) {
                     $this->log .= '- add watermark<br />';
                     $this->image_watermark_position = strtolower($this->image_watermark_position);
                     $watermark_info = getimagesize($this->image_watermark);
                     $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : null;
                     // 1 = GIF, 2 = JPG, 3 = PNG
                     $watermark_checked = false;
                     if ($watermark_type == IMAGETYPE_GIF) {
                         if (!function_exists('imagecreatefromgif')) {
                             $this->error = $this->translate('watermark_no_create_support', array('GIF'));
                         } else {
                             $filter = @imagecreatefromgif($this->image_watermark);
                             if (!$filter) {
                                 $this->error = $this->translate('watermark_create_error', array('GIF'));
                             } else {
                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is GIF<br />';
                                 $watermark_checked = true;
                             }
                         }
                     } else {
                         if ($watermark_type == IMAGETYPE_JPEG) {
                             if (!function_exists('imagecreatefromjpeg')) {
                                 $this->error = $this->translate('watermark_no_create_support', array('JPEG'));
                             } else {
                                 $filter = @imagecreatefromjpeg($this->image_watermark);
                                 if (!$filter) {
                                     $this->error = $this->translate('watermark_create_error', array('JPEG'));
                                 } else {
                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is JPEG<br />';
                                     $watermark_checked = true;
                                 }
                             }
                         } else {
                             if ($watermark_type == IMAGETYPE_PNG) {
                                 if (!function_exists('imagecreatefrompng')) {
                                     $this->error = $this->translate('watermark_no_create_support', array('PNG'));
                                 } else {
                                     $filter = @imagecreatefrompng($this->image_watermark);
                                     if (!$filter) {
                                         $this->error = $this->translate('watermark_create_error', array('PNG'));
                                     } else {
                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is PNG<br />';
                                         $watermark_checked = true;
                                     }
                                 }
                             } else {
                                 if ($watermark_type == IMAGETYPE_BMP) {
                                     if (!method_exists($this, 'imagecreatefrombmp')) {
                                         $this->error = $this->translate('watermark_no_create_support', array('BMP'));
                                     } else {
                                         $filter = @$this->imagecreatefrombmp($this->image_watermark);
                                         if (!$filter) {
                                             $this->error = $this->translate('watermark_create_error', array('BMP'));
                                         } else {
                                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is BMP<br />';
                                             $watermark_checked = true;
                                         }
                                     }
                                 } else {
                                     $this->error = $this->translate('watermark_invalid');
                                 }
                             }
                         }
                     }
                     if ($watermark_checked) {
                         $watermark_dst_width = $watermark_src_width = imagesx($filter);
                         $watermark_dst_height = $watermark_src_height = imagesy($filter);
                         // if watermark is too large/tall, resize it first
                         if (!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y) || !$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y) {
                             $canvas_width = $this->image_dst_x - abs($this->image_watermark_x);
                             $canvas_height = $this->image_dst_y - abs($this->image_watermark_y);
                             if ($watermark_src_width / $canvas_width > $watermark_src_height / $canvas_height) {
                                 $watermark_dst_width = $canvas_width;
                                 $watermark_dst_height = intval($watermark_src_height * ($canvas_width / $watermark_src_width));
                             } else {
                                 $watermark_dst_height = $canvas_height;
                                 $watermark_dst_width = intval($watermark_src_width * ($canvas_height / $watermark_src_height));
                             }
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark resized from ' . $watermark_src_width . 'x' . $watermark_src_height . ' to ' . $watermark_dst_width . 'x' . $watermark_dst_height . '<br />';
                         }
                         // determine watermark position
                         $watermark_x = 0;
                         $watermark_y = 0;
                         if (is_numeric($this->image_watermark_x)) {
                             if ($this->image_watermark_x < 0) {
                                 $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x;
                             } else {
                                 $watermark_x = $this->image_watermark_x;
                             }
                         } else {
                             if (strpos($this->image_watermark_position, 'r') !== false) {
                                 $watermark_x = $this->image_dst_x - $watermark_dst_width;
                             } else {
                                 if (strpos($this->image_watermark_position, 'l') !== false) {
                                     $watermark_x = 0;
                                 } else {
                                     $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2;
                                 }
                             }
                         }
                         if (is_numeric($this->image_watermark_y)) {
                             if ($this->image_watermark_y < 0) {
                                 $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y;
                             } else {
                                 $watermark_y = $this->image_watermark_y;
                             }
                         } else {
                             if (strpos($this->image_watermark_position, 'b') !== false) {
                                 $watermark_y = $this->image_dst_y - $watermark_dst_height;
                             } else {
                                 if (strpos($this->image_watermark_position, 't') !== false) {
                                     $watermark_y = 0;
                                 } else {
                                     $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2;
                                 }
                             }
                         }
                         imagealphablending($image_dst, true);
                         imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height);
                     } else {
                         $this->error = $this->translate('watermark_invalid');
                     }
                 }
                 // add text
                 if (!empty($this->image_text)) {
                     $this->log .= '- add text<br />';
                     // calculate sizes in human readable format
                     $src_size = $this->file_src_size / 1024;
                     $src_size_mb = number_format($src_size / 1024, 1, ".", " ");
                     $src_size_kb = number_format($src_size, 1, ".", " ");
                     $src_size_human = $src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb";
                     $this->image_text = str_replace(array('[src_name]', '[src_name_body]', '[src_name_ext]', '[src_pathname]', '[src_mime]', '[src_size]', '[src_size_kb]', '[src_size_mb]', '[src_size_human]', '[src_x]', '[src_y]', '[src_pixels]', '[src_type]', '[src_bits]', '[dst_path]', '[dst_name_body]', '[dst_name_ext]', '[dst_name]', '[dst_pathname]', '[dst_x]', '[dst_y]', '[date]', '[time]', '[host]', '[server]', '[ip]', '[gd_version]'), array($this->file_src_name, $this->file_src_name_body, $this->file_src_name_ext, $this->file_src_pathname, $this->file_src_mime, $this->file_src_size, $src_size_kb, $src_size_mb, $src_size_human, $this->image_src_x, $this->image_src_y, $this->image_src_pixels, $this->image_src_type, $this->image_src_bits, $this->file_dst_path, $this->file_dst_name_body, $this->file_dst_name_ext, $this->file_dst_name, $this->file_dst_pathname, $this->image_dst_x, $this->image_dst_y, date('Y-m-d'), date('H:i:s'), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a', $this->gdversion(true)), $this->image_text);
                     if (!is_numeric($this->image_text_padding)) {
                         $this->image_text_padding = 0;
                     }
                     if (!is_numeric($this->image_text_line_spacing)) {
                         $this->image_text_line_spacing = 0;
                     }
                     if (!is_numeric($this->image_text_padding_x)) {
                         $this->image_text_padding_x = $this->image_text_padding;
                     }
                     if (!is_numeric($this->image_text_padding_y)) {
                         $this->image_text_padding_y = $this->image_text_padding;
                     }
                     $this->image_text_position = strtolower($this->image_text_position);
                     $this->image_text_direction = strtolower($this->image_text_direction);
                     $this->image_text_alignment = strtolower($this->image_text_alignment);
                     // if the font is a string, we assume that we might want to load a font
                     if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') {
                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;try to load font ' . $this->image_text_font . '... ';
                         if ($this->image_text_font = @imageloadfont($this->image_text_font)) {
                             $this->log .= 'success<br />';
                         } else {
                             $this->log .= 'error<br />';
                             $this->image_text_font = 5;
                         }
                     }
                     $text = explode("\n", $this->image_text);
                     $char_width = imagefontwidth($this->image_text_font);
                     $char_height = imagefontheight($this->image_text_font);
                     $text_height = 0;
                     $text_width = 0;
                     $line_height = 0;
                     $line_width = 0;
                     foreach ($text as $k => $v) {
                         if ($this->image_text_direction == 'v') {
                             $h = $char_width * strlen($v);
                             if ($h > $text_height) {
                                 $text_height = $h;
                             }
                             $line_width = $char_height;
                             $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0);
                         } else {
                             $w = $char_width * strlen($v);
                             if ($w > $text_width) {
                                 $text_width = $w;
                             }
                             $line_height = $char_height;
                             $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0);
                         }
                     }
                     $text_width += 2 * $this->image_text_padding_x;
                     $text_height += 2 * $this->image_text_padding_y;
                     $text_x = 0;
                     $text_y = 0;
                     if (is_numeric($this->image_text_x)) {
                         if ($this->image_text_x < 0) {
                             $text_x = $this->image_dst_x - $text_width + $this->image_text_x;
                         } else {
                             $text_x = $this->image_text_x;
                         }
                     } else {
                         if (strpos($this->image_text_position, 'r') !== false) {
                             $text_x = $this->image_dst_x - $text_width;
                         } else {
                             if (strpos($this->image_text_position, 'l') !== false) {
                                 $text_x = 0;
                             } else {
                                 $text_x = ($this->image_dst_x - $text_width) / 2;
                             }
                         }
                     }
                     if (is_numeric($this->image_text_y)) {
                         if ($this->image_text_y < 0) {
                             $text_y = $this->image_dst_y - $text_height + $this->image_text_y;
                         } else {
                             $text_y = $this->image_text_y;
                         }
                     } else {
                         if (strpos($this->image_text_position, 'b') !== false) {
                             $text_y = $this->image_dst_y - $text_height;
                         } else {
                             if (strpos($this->image_text_position, 't') !== false) {
                                 $text_y = 0;
                             } else {
                                 $text_y = ($this->image_dst_y - $text_height) / 2;
                             }
                         }
                     }
                     // add a background, maybe transparent
                     if (!empty($this->image_text_background)) {
                         list($red, $green, $blue) = $this->getcolors($this->image_text_background);
                         if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) {
                             $filter = imagecreatetruecolor($text_width, $text_height);
                             $background_color = imagecolorallocate($filter, $red, $green, $blue);
                             imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color);
                             $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent);
                             imagedestroy($filter);
                         } else {
                             $background_color = imagecolorallocate($image_dst, $red, $green, $blue);
                             imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color);
                         }
                     }
                     $text_x += $this->image_text_padding_x;
                     $text_y += $this->image_text_padding_y;
                     $t_width = $text_width - 2 * $this->image_text_padding_x;
                     $t_height = $text_height - 2 * $this->image_text_padding_y;
                     list($red, $green, $blue) = $this->getcolors($this->image_text_color);
                     // add the text, maybe transparent
                     if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) {
                         if ($t_width < 0) {
                             $t_width = 0;
                         }
                         if ($t_height < 0) {
                             $t_height = 0;
                         }
                         $filter = $this->imagecreatenew($t_width, $t_height, false, true);
                         $text_color = imagecolorallocate($filter, $red, $green, $blue);
                         foreach ($text as $k => $v) {
                             if ($this->image_text_direction == 'v') {
                                 imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color);
                             } else {
                                 imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color);
                             }
                         }
                         $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent);
                         imagedestroy($filter);
                     } else {
                         $text_color = imageColorAllocate($image_dst, $red, $green, $blue);
                         foreach ($text as $k => $v) {
                             if ($this->image_text_direction == 'v') {
                                 imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color);
                             } else {
                                 imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color);
                             }
                         }
                     }
                 }
                 // add a reflection
                 if ($this->image_reflection_height) {
                     $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />';
                     // we decode image_reflection_height, which can be a integer, a string in pixels or percentage
                     $image_reflection_height = $this->image_reflection_height;
                     if (strpos($image_reflection_height, '%') > 0) {
                         $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100);
                     }
                     if (strpos($image_reflection_height, 'px') > 0) {
                         $image_reflection_height = str_replace('px', '', $image_reflection_height);
                     }
                     $image_reflection_height = (int) $image_reflection_height;
                     if ($image_reflection_height > $this->image_dst_y) {
                         $image_reflection_height = $this->image_dst_y;
                     }
                     if (empty($this->image_reflection_opacity)) {
                         $this->image_reflection_opacity = 60;
                     }
                     // create the new destination image
                     $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true);
                     $transparency = $this->image_reflection_opacity;
                     // copy the original image
                     imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0));
                     // we have to make sure the extra bit is the right color, or transparent
                     if ($image_reflection_height + $this->image_reflection_space > 0) {
                         // use the background color if present
                         if (!empty($this->image_background_color)) {
                             list($red, $green, $blue) = $this->getcolors($this->image_background_color);
                             $fill = imagecolorallocate($tmp, $red, $green, $blue);
                         } else {
                             $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
                         }
                         // fill in from the edge of the extra bit
                         imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill);
                     }
                     // copy the reflection
                     for ($y = 0; $y < $image_reflection_height; $y++) {
                         for ($x = 0; $x < $this->image_dst_x; $x++) {
                             $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space));
                             $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)));
                             $alpha_o = 1 - $pixel_o['alpha'] / 127;
                             $alpha_b = 1 - $pixel_b['alpha'] / 127;
                             $opacity = $alpha_o * $transparency / 100;
                             if ($opacity > 0) {
                                 $red = round(($pixel_o['red'] * $opacity + $pixel_b['red'] * $alpha_b) / ($alpha_b + $opacity));
                                 $green = round(($pixel_o['green'] * $opacity + $pixel_b['green'] * $alpha_b) / ($alpha_b + $opacity));
                                 $blue = round(($pixel_o['blue'] * $opacity + $pixel_b['blue'] * $alpha_b) / ($alpha_b + $opacity));
                                 $alpha = $opacity + $alpha_b;
                                 if ($alpha > 1) {
                                     $alpha = 1;
                                 }
                                 $alpha = round((1 - $alpha) * 127);
                                 $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha);
                                 imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color);
                             }
                         }
                         if ($transparency > 0) {
                             $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height;
                         }
                     }
                     // copy the resulting image into the destination image
                     $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space;
                     $image_dst = $this->imagetransfer($tmp, $image_dst);
                 }
                 // reduce the JPEG image to a set desired size
                 if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
                     // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
                     $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />';
                     // calculate size of each image. 75%, 50%, and 25% quality
                     ob_start();
                     imagejpeg($image_dst, '', 75);
                     $buffer = ob_get_contents();
                     ob_end_clean();
                     $size75 = strlen($buffer);
                     ob_start();
                     imagejpeg($image_dst, '', 50);
                     $buffer = ob_get_contents();
                     ob_end_clean();
                     $size50 = strlen($buffer);
                     ob_start();
                     imagejpeg($image_dst, '', 25);
                     $buffer = ob_get_contents();
                     ob_end_clean();
                     $size25 = strlen($buffer);
                     // calculate gradient of size reduction by quality
                     $mgrad1 = 25 / ($size50 - $size25);
                     $mgrad2 = 25 / ($size75 - $size50);
                     $mgrad3 = 50 / ($size75 - $size25);
                     $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3;
                     // result of approx. quality factor for expected size
                     $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50);
                     if ($q_factor < 1) {
                         $this->jpeg_quality = 1;
                     } elseif ($q_factor > 100) {
                         $this->jpeg_quality = 100;
                     } else {
                         $this->jpeg_quality = $q_factor;
                     }
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG quality factor set to ' . $this->jpeg_quality . '<br />';
                 }
                 // converts image from true color, and fix transparency if needed
                 $this->log .= '- converting...<br />';
                 switch ($this->image_convert) {
                     case 'gif':
                         // if the image is true color, we convert it to a palette
                         if (imageistruecolor($image_dst)) {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;true color to palette<br />';
                             // creates a black and white mask
                             $mask = array(array());
                             for ($x = 0; $x < $this->image_dst_x; $x++) {
                                 for ($y = 0; $y < $this->image_dst_y; $y++) {
                                     $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                     $mask[$x][$y] = $pixel['alpha'];
                                 }
                             }
                             list($red, $green, $blue) = $this->getcolors($this->image_default_color);
                             // first, we merge the image with the background color, so we know which colors we will have
                             for ($x = 0; $x < $this->image_dst_x; $x++) {
                                 for ($y = 0; $y < $this->image_dst_y; $y++) {
                                     if ($mask[$x][$y] > 0) {
                                         // we have some transparency. we combine the color with the default color
                                         $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                         $alpha = $mask[$x][$y] / 127;
                                         $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha);
                                         $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha);
                                         $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha);
                                         $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
                                         imagesetpixel($image_dst, $x, $y, $color);
                                     }
                                 }
                             }
                             // transforms the true color image into palette, with its merged default color
                             if (empty($this->image_background_color)) {
                                 imagetruecolortopalette($image_dst, true, 255);
                                 $transparency = imagecolorallocate($image_dst, 254, 1, 253);
                                 imagecolortransparent($image_dst, $transparency);
                                 // make the transparent areas transparent
                                 for ($x = 0; $x < $this->image_dst_x; $x++) {
                                     for ($y = 0; $y < $this->image_dst_y; $y++) {
                                         // we test wether we have enough opacity to justify keeping the color
                                         if ($mask[$x][$y] > 120) {
                                             imagesetpixel($image_dst, $x, $y, $transparency);
                                         }
                                     }
                                 }
                             }
                             unset($mask);
                         }
                         break;
                     case 'jpg':
                     case 'bmp':
                         // if the image doesn't support any transparency, then we merge it with the default color
                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;fills in transparency with default color<br />';
                         list($red, $green, $blue) = $this->getcolors($this->image_default_color);
                         $transparency = imagecolorallocate($image_dst, $red, $green, $blue);
                         // make the transaparent areas transparent
                         for ($x = 0; $x < $this->image_dst_x; $x++) {
                             for ($y = 0; $y < $this->image_dst_y; $y++) {
                                 // we test wether we have some transparency, in which case we will merge the colors
                                 if (imageistruecolor($image_dst)) {
                                     $rgba = imagecolorat($image_dst, $x, $y);
                                     $pixel = array('red' => $rgba >> 16 & 0xff, 'green' => $rgba >> 8 & 0xff, 'blue' => $rgba & 0xff, 'alpha' => ($rgba & 0x7f000000) >> 24);
                                 } else {
                                     $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
                                 }
                                 if ($pixel['alpha'] == 127) {
                                     // we have full transparency. we make the pixel transparent
                                     imagesetpixel($image_dst, $x, $y, $transparency);
                                 } else {
                                     if ($pixel['alpha'] > 0) {
                                         // we have some transparency. we combine the color with the default color
                                         $alpha = $pixel['alpha'] / 127;
                                         $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha);
                                         $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha);
                                         $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha);
                                         $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
                                         imagesetpixel($image_dst, $x, $y, $color);
                                     }
                                 }
                             }
                         }
                         break;
                     default:
                         break;
                 }
                 // outputs image
                 $this->log .= '- saving image...<br />';
                 switch ($this->image_convert) {
                     case 'jpeg':
                     case 'jpg':
                         if (!$return_mode) {
                             $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality);
                         } else {
                             ob_start();
                             $result = @imagejpeg($image_dst, '', $this->jpeg_quality);
                             $return_content = ob_get_contents();
                             ob_end_clean();
                         }
                         if (!$result) {
                             $this->processed = false;
                             $this->error = $this->translate('file_create', array('JPEG'));
                         } else {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG image created<br />';
                         }
                         break;
                     case 'png':
                         imagealphablending($image_dst, false);
                         imagesavealpha($image_dst, true);
                         if (!$return_mode) {
                             $result = @imagepng($image_dst, $this->file_dst_pathname);
                         } else {
                             ob_start();
                             $result = @imagepng($image_dst);
                             $return_content = ob_get_contents();
                             ob_end_clean();
                         }
                         if (!$result) {
                             $this->processed = false;
                             $this->error = $this->translate('file_create', array('PNG'));
                         } else {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PNG image created<br />';
                         }
                         break;
                     case 'gif':
                         if (!$return_mode) {
                             $result = @imagegif($image_dst, $this->file_dst_pathname);
                         } else {
                             ob_start();
                             $result = @imagegif($image_dst);
                             $return_content = ob_get_contents();
                             ob_end_clean();
                         }
                         if (!$result) {
                             $this->processed = false;
                             $this->error = $this->translate('file_create', array('GIF'));
                         } else {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;GIF image created<br />';
                         }
                         break;
                     case 'bmp':
                         if (!$return_mode) {
                             $result = $this->imagebmp($image_dst, $this->file_dst_pathname);
                         } else {
                             ob_start();
                             $result = $this->imagebmp($image_dst);
                             $return_content = ob_get_contents();
                             ob_end_clean();
                         }
                         if (!$result) {
                             $this->processed = false;
                             $this->error = $this->translate('file_create', array('BMP'));
                         } else {
                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;BMP image created<br />';
                         }
                         break;
                     default:
                         $this->processed = false;
                         $this->error = $this->translate('no_conversion_type');
                 }
                 if ($this->processed) {
                     if (is_resource($image_src)) {
                         imagedestroy($image_src);
                     }
                     if (is_resource($image_dst)) {
                         imagedestroy($image_dst);
                     }
                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image objects destroyed<br />';
                 }
             }
         } else {
             $this->log .= '- no image processing wanted<br />';
             if (!$return_mode) {
                 // copy the file to its final destination. we don't use move_uploaded_file here
                 // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
                 if (!copy($this->file_src_pathname, $this->file_dst_pathname)) {
                     $this->processed = false;
                     $this->error = $this->translate('copy_failed');
                 }
             } else {
                 // returns the file, so that its content can be received by the caller
                 $return_content = @file_get_contents($this->file_src_pathname);
                 if ($return_content === FALSE) {
                     $this->processed = false;
                     $this->error = $this->translate('reading_failed');
                 }
             }
         }
     }
     if ($this->processed) {
         $this->log .= '- <b>process OK</b><br />';
     } else {
         $this->log .= '- <b>error</b>: ' . $this->error . '<br />';
     }
     // we reinit all the vars
     $this->init();
     // we may return the image content
     if ($return_mode) {
         return $return_content;
     }
 }
Example #29
0
 function _AdjBrightContrast($img, $bright, $contr = 0)
 {
     if ($bright < -1 || $bright > 1 || $contr < -1 || $contr > 1) {
         JpGraphError::Raise(" Parameters for brightness and Contrast out of range [-1,1]");
     }
     $nbr = imagecolorstotal($img);
     for ($i = 0; $i < $nbr; ++$i) {
         $colarr = imagecolorsforindex($img, $i);
         $r = $this->AdjRGBBrightContrast($colarr["red"], $bright, $contr);
         $g = $this->AdjRGBBrightContrast($colarr["green"], $bright, $contr);
         $b = $this->AdjRGBBrightContrast($colarr["blue"], $bright, $contr);
         imagecolorset($img, $i, $r, $g, $b);
     }
 }
Example #30
0
 function uw_image_resize($url, $width = NULL, $height = NULL, $crop = true, $retina = false)
 {
     global $wpdb;
     if (empty($url)) {
         return new WP_Error('no_image_url', __('No image URL has been entered.', 'wta'), $url);
     }
     // Bail if GD Library doesn't exist
     if (!extension_loaded('gd') || !function_exists('gd_info')) {
         return array('url' => $url, 'width' => $width, 'height' => $height);
     }
     // Get default size from database
     $width = $width ? $width : get_option('thumbnail_size_w');
     $height = $height ? $height : get_option('thumbnail_size_h');
     // Allow for different retina sizes
     $retina = $retina ? $retina === true ? 2 : $retina : 1;
     // Destination width and height variables
     $dest_width = $width * $retina;
     $dest_height = $height * $retina;
     // Get image file path
     $file_path = parse_url($url);
     $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path['path'];
     // Check for Multisite
     if (is_multisite()) {
         global $blog_id;
         $blog_details = get_blog_details($blog_id);
         $file_path = str_replace($blog_details->path . 'files/', '/wp-content/blogs.dir/' . $blog_id . '/files/', $file_path);
     }
     // Some additional info about the image
     $info = pathinfo($file_path);
     $dir = $info['dirname'];
     $ext = $info['extension'];
     $name = wp_basename($file_path, ".{$ext}");
     if ('bmp' == $ext) {
         return new WP_Error('bmp_mime_type', __('Image is BMP. Please use either JPG or PNG.', 'wta'), $url);
     }
     // Suffix applied to filename
     $suffix = "{$dest_width}x{$dest_height}";
     // Get the destination file name
     $dest_file_name = "{$dir}/{$name}-{$suffix}.{$ext}";
     // No need to resize & create a new image if it already exists!
     if (!file_exists($dest_file_name)) {
         /*
          *  Bail if this image isn't in the Media Library either.
          *  We only want to resize Media Library images, so we can be sure they get deleted correctly when appropriate.
          */
         $query = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE guid='%s'", $url);
         $get_attachment = $wpdb->get_results($query);
         if (!$get_attachment) {
             return array('url' => $url, 'width' => $width, 'height' => $height);
         }
         $image = wp_load_image($file_path);
         if (!is_resource($image)) {
             return new WP_Error('error_loading_image_as_resource', $image, $file_path);
         }
         // Get the current image dimensions and type
         $size = @getimagesize($file_path);
         if (!$size) {
             return new WP_Error('file_path_getimagesize_failed', __('Failed to get $file_path information using "@getimagesize".', 'wta'), $file_path);
         }
         list($orig_width, $orig_height, $orig_type) = $size;
         // Create new image
         $new_image = wp_imagecreatetruecolor($dest_width, $dest_height);
         // Do some proportional cropping if enabled
         if ($crop) {
             $src_x = $src_y = 0;
             $src_w = $orig_width;
             $src_h = $orig_height;
             $cmp_x = $orig_width / $dest_width;
             $cmp_y = $orig_height / $dest_height;
             // Calculate x or y coordinate, and width or height of source
             if ($cmp_x > $cmp_y) {
                 $src_w = round($orig_width / $cmp_x * $cmp_y);
                 $src_x = round(($orig_width - $orig_width / $cmp_x * $cmp_y) / 2);
             } else {
                 if ($cmp_y > $cmp_x) {
                     $src_h = round($orig_height / $cmp_y * $cmp_x);
                     $src_y = round(($orig_height - $orig_height / $cmp_y * $cmp_x) / 2);
                 }
             }
             // Create the resampled image
             imagecopyresampled($new_image, $image, 0, 0, $src_x, $src_y, $dest_width, $dest_height, $src_w, $src_h);
         } else {
             imagecopyresampled($new_image, $image, 0, 0, 0, 0, $dest_width, $dest_height, $orig_width, $orig_height);
         }
         // Convert from full colors to index colors, like original PNG.
         if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($image)) {
             imagetruecolortopalette($new_image, false, imagecolorstotal($image));
         }
         // Remove the original image from memory (no longer needed)
         imagedestroy($image);
         // Check the image is the correct file type
         if (IMAGETYPE_GIF == $orig_type) {
             if (!imagegif($new_image, $dest_file_name)) {
                 return new WP_Error('resize_path_invalid', __('Resize path invalid (GIF)', 'wta'));
             }
         } elseif (IMAGETYPE_PNG == $orig_type) {
             if (!imagepng($new_image, $dest_file_name)) {
                 return new WP_Error('resize_path_invalid', __('Resize path invalid (PNG).', 'wta'));
             }
         } else {
             // All other formats are converted to jpg
             if ('jpg' != $ext && 'jpeg' != $ext) {
                 $dest_file_name = "{$dir}/{$name}-{$suffix}.jpg";
             }
             if (!imagejpeg($new_image, $dest_file_name, apply_filters('resize_jpeg_quality', 90))) {
                 return new WP_Error('resize_path_invalid', __('Resize path invalid (JPG).', 'wta'));
             }
         }
         // Remove new image from memory (no longer needed as well)
         imagedestroy($new_image);
         // Set correct file permissions
         $stat = stat(dirname($dest_file_name));
         $perms = $stat['mode'] & 0666;
         @chmod($dest_file_name, $perms);
         // Get some information about the resized image
         $new_size = @getimagesize($dest_file_name);
         if (!$new_size) {
             return new WP_Error('resize_path_getimagesize_failed', __('Failed to get $dest_file_name (resized image) info via @getimagesize', 'wta'), $dest_file_name);
         }
         list($resized_width, $resized_height, $resized_type) = $new_size;
         // Get the new image URL
         $resized_url = str_replace(basename($url), basename($dest_file_name), $url);
         // Add the resized dimensions to original image metadata (so we can delete our resized images when the original image is delete from the Media Library)
         $metadata = wp_get_attachment_metadata($get_attachment[0]->ID);
         if (isset($metadata['image_meta'])) {
             $metadata['image_meta']['resized_images'][] = $resized_width . 'x' . $resized_height;
             wp_update_attachment_metadata($get_attachment[0]->ID, $metadata);
         }
         // Return array with resized image information
         $image_array = array('url' => $resized_url, 'width' => $resized_width, 'height' => $resized_height, 'type' => $resized_type);
     } else {
         $image_array = array('url' => str_replace(basename($url), basename($dest_file_name), $url), 'width' => $dest_width, 'height' => $dest_height, 'type' => $ext);
     }
     return $image_array;
 }