Exemple #1
0
function compressImage($ext, $uploadedfile, $path, $actual_image_name, $newwidth)
{
    if ($ext == "jpg" || $ext == "jpeg") {
        $src = imagecreatefromjpeg($uploadedfile);
    } else {
        if ($ext == "png") {
            $src = imagecreatefrompng($uploadedfile);
        } else {
            if ($ext == "gif") {
                $src = imagecreatefromgif($uploadedfile);
            } else {
                $src = imagecreatefrombmp($uploadedfile);
            }
        }
    }
    list($width, $height) = getimagesize($uploadedfile);
    $newheight = $height / $width * $newwidth;
    $tmp = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    $filename = $path . $newwidth . '_' . $actual_image_name;
    //PixelSize_TimeStamp.jpg
    imagejpeg($tmp, $filename, 100);
    imagedestroy($tmp);
    return $filename;
}
Exemple #2
0
 function load($img_name, $img_type = '')
 {
     if (!empty($img_type)) {
         $this->_imagetype = $img_type;
     } else {
         $this->_imagetype = $this->get_type($img_name);
     }
     switch ($this->_imagetype) {
         case 'gif':
             if (function_exists('imagecreatefromgif')) {
                 $this->_img = imagecreatefromgif($img_name);
             }
             break;
         case 'jpg':
         case 'jpeg':
             $this->_img = imagecreatefromjpeg($img_name);
             break;
         case 'bmp':
             include_once dirname(__FILE__) . '/gdbmp.php';
             $this->_img = imagecreatefrombmp($img_name);
             break;
         case 'png':
             $this->_img = imagecreatefrompng($img_name);
             break;
         default:
             $this->_img = imagecreatefromstring($img_name);
             break;
     }
     $this->getxy();
 }
 private function ConvertImage($originalImage, $outputImage, $quality)
 {
     // jpg, png, gif or bmp?
     $exploded = explode('.', $originalImage);
     $ext = $exploded[count($exploded) - 1];
     if (preg_match('/jpg|jpeg/i', $ext)) {
         $imageTmp = imagecreatefromjpeg($originalImage);
     } else {
         if (preg_match('/png/i', $ext)) {
             $imageTmp = imagecreatefrompng($originalImage);
         } else {
             if (preg_match('/gif/i', $ext)) {
                 $imageTmp = imagecreatefromgif($originalImage);
             } else {
                 if (preg_match('/bmp/i', $ext)) {
                     $imageTmp = imagecreatefrombmp($originalImage);
                 } else {
                     return 0;
                 }
             }
         }
     }
     // quality is a value from 0 (worst) to 100 (best)
     imagejpeg($imageTmp, $outputImage, $quality);
     imagedestroy($imageTmp);
     return 1;
 }
function convertImage($originalImage, $outputImage, $quality = 100)
{
    $myfile = fopen($outputImage, "w");
    // jpg, png, gif or bmp?
    $exploded = explode('.', $originalImage);
    $ext = $exploded[count($exploded) - 1];
    if (preg_match('/jpg|jpeg/i', $ext)) {
        $imageTmp = imagecreatefromjpeg($originalImage);
    } else {
        if (preg_match('/png/i', $ext)) {
            $imageTmp = imagecreatefrompng($originalImage);
        } else {
            if (preg_match('/gif/i', $ext)) {
                $imageTmp = imagecreatefromgif($originalImage);
            } else {
                if (preg_match('/bmp/i', $ext)) {
                    $imageTmp = imagecreatefrombmp($originalImage);
                } else {
                    return 0;
                }
            }
        }
    }
    imagejpeg($imageTmp, $outputImage, $quality);
    imagedestroy($imageTmp);
    return $outputImage;
}
 public function load($path, $realname = null)
 {
     if (!file_exists($path)) {
         return false;
     }
     if ($realname == null) {
         $pinfo = pathinfo($path);
         $ext = $pinfo["extension"];
     } else {
         $pinfo = pathinfo($realname);
         $ext = $pinfo["extension"];
     }
     switch (strtolower($ext)) {
         case "jpeg":
         case "jpg":
             $this->image = imagecreatefromjpeg($path);
             break;
         case "gif":
             $this->image = imagecreatefromgif($path);
             break;
         case "png":
             $this->image = imagecreatefrompng($path);
             break;
         case "bmp":
             $this->image = imagecreatefrombmp($path);
             break;
     }
     if ($this->image == null) {
         return false;
     }
     return true;
 }
Exemple #6
0
function imageresize_temp($image_url, $extension, $image_width = "260")
{
    switch ($extension) {
        case 'jpeg':
            $simg = imagecreatefromjpeg($image_url);
            break;
        case 'jpg':
            $simg = imagecreatefromjpeg($image_url);
            break;
        case 'png':
            $simg = imagecreatefrompng($image_url);
            break;
        case 'bmp':
            $simg = imagecreatefrombmp($image_url);
            break;
        case 'gif':
            $simg = imagecreatefromgif($image_url);
            break;
        default:
            return false;
    }
    $currwidth = imagesx($simg);
    $currheight = imagesy($simg);
    $zoom = $image_width / $currwidth;
    $newwidth = $image_width;
    $newheight = $currheight * $zoom;
    $dimg = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
    $filename = '/tmp/' . time() . '.' . $extension;
    imagejpeg($dimg, $filename);
    imagedestroy($simg);
    imagedestroy($dimg);
    return $filename;
}
Exemple #7
0
function convertImage($originalImage, $outputImage, $quality)
{
    $exploded = explode('.', $originalImage);
    $ext = $exploded[count($exploded) - 1];
    if (preg_match('/jpg|jpeg/i', $ext)) {
        $imageTmp = imagecreatefromjpeg($originalImage);
    } else {
        if (preg_match('/png/i', $ext)) {
            $imageTmp = imagecreatefrompng($originalImage);
        } else {
            if (preg_match('/gif/i', $ext)) {
                $imageTmp = imagecreatefromgif($originalImage);
            } else {
                if (preg_match('/bmp/i', $ext)) {
                    $imageTmp = imagecreatefrombmp($originalImage);
                } else {
                    return 0;
                }
            }
        }
    }
    imagejpeg($imageTmp, $outputImage, $quality);
    imagedestroy($imageTmp);
    print $outputImage;
}
Exemple #8
0
function open_image($file)
{
    list($width, $height, $type, $attr) = getimagesize($file);
    //echo "Type: ".$type."<br />";
    // http://www.php.net/manual/en/function.exif-imagetype.php
    /*1	IMAGETYPE_GIF
      2	IMAGETYPE_JPEG
      3	IMAGETYPE_PNG
      4	IMAGETYPE_SWF
      5	IMAGETYPE_PSD
      6	IMAGETYPE_BMP
      7	IMAGETYPE_TIFF_II (intel byte order)
      8	IMAGETYPE_TIFF_MM (motorola byte order)
      9	IMAGETYPE_JPC
      10	IMAGETYPE_JP2
      11	IMAGETYPE_JPX
      12	IMAGETYPE_JB2
      13	IMAGETYPE_SWC
      14	IMAGETYPE_IFF
      15	IMAGETYPE_WBMP
      16	IMAGETYPE_XBM
      17	IMAGETYPE_ICO */
    if ($type == 2) {
        $im = @imagecreatefromjpeg($file);
    } elseif ($type == 1) {
        $im = @imagecreatefromgif($file);
    } elseif ($type == 3) {
        $im = @imagecreatefrompng($file);
    } elseif ($type == 15) {
        $im = @imagecreatefromwbmp($file);
    } elseif ($type == 6) {
        $im = @imagecreatefrombmp($file);
    } elseif ($type == 16) {
        $im = @imagecreatefromxbm($file);
    } else {
        $im = @imagecreatefromstring(file_get_contents($file));
    }
    /*if ( $type == IMAGETYPE_JPEG ) {
         	$im = @imagecreatefromjpeg($file); 
     	} elseif ( $type == IMAGETYPE_GIF ) {
         	$im = @imagecreatefromgif($file);
     	} elseif ( $type == IMAGETYPE_PNG ) {
         	$im = @imagecreatefrompng($file);
     	} elseif ( $type == IMAGETYPE_WBMP ) {
         	$im = @imagecreatefromwbmp($file);
     	} elseif ( $type == IMAGETYPE_BMP ) {
         	$im = @imagecreatefrombmp($file);
     	} elseif ( $type == IMAGETYPE_XBM ) {
         	$im = @imagecreatefromxbm($file);
     	} else {
         	$im = @imagecreatefromstring(file_get_contents($file));
     	}*/
    if ($im !== false) {
        return $im;
    } else {
        die(throwError("Unable to open_image"));
    }
    return false;
}
function makeThumb($path, $size, $mime = FALSE)
{
    // Verifica se é uma imagem
    // @since rev 1
    if ($mime === FALSE) {
        $mime = mime_content_type($path);
    }
    if (strpos($mime, "jpeg") !== FALSE) {
        $buffer = imagecreatefromjpeg($path);
    } elseif (strpos($mime, "bmp") !== FALSE) {
        $buffer = imagecreatefrombmp($path);
    } elseif (strpos($mime, "gif") !== FALSE) {
        $buffer = imagecreatefromgif($path);
    } elseif (strpos($mime, "png") !== FALSE) {
        $buffer = imagecreatefrompng($path);
    } else {
        return FALSE;
    }
    if ($buffer === FALSE) {
        return FALSE;
    }
    // Busca o tamanho da imagem
    // @since rev 1
    $x = $origem_x = ImagesX($buffer);
    $y = $origem_y = ImagesY($buffer);
    // Verifica qual deve ser a proporção
    // @since rev 1
    if ($x >= $y) {
        if ($x > $size) {
            $x1 = (int) ($x * ($size / $x));
            $y1 = (int) ($y * ($size / $x));
        } else {
            $x1 = $x;
            $y1 = $y;
        }
    } else {
        if ($y > $size) {
            $x1 = (int) ($x * ($size / $y));
            $y1 = (int) ($y * ($size / $y));
        } else {
            $x1 = $x;
            $y1 = $y;
        }
    }
    // Muda o tamanho da imagem
    // @since rev 1
    $image = ImageCreateTrueColor($x1, $y1);
    if ($image === FALSE) {
        return FALSE;
    }
    ImageCopyResampled($image, $buffer, 0, 0, 0, 0, $x1 + 1, $y1 + 1, $origem_x, $origem_y);
    // Libera recursos
    // @since rev 1
    ImageDestroy($buffer);
    // Retorna o resource
    // @since rev 1
    return $image;
}
Exemple #10
0
 /**
  *  Para insertar un nuevo registro, debo pasar la ruta de
  *  una imagen válida (puede ser de un archivo local o uno remoto con http://...)
  */
 public static function INSERT($image_path)
 {
     // Compruebo si el archivo es en realidad una imagen:
     //$finfo = finfo_open(FILEINFO_MIME_TYPE);
     //$mime = finfo_file($finfo, $image_path);
     $temp_hash = md5(microtime());
     Rack::Write('temp', $temp_hash, $image_path);
     $temp_path = Rack::Path('temp', $temp_hash);
     $is = getimagesize($temp_path);
     $mime = $is['mime'];
     switch ($mime) {
         case 'image/jpeg':
             $gd = @imagecreatefromjpeg($temp_path);
             break;
         case 'image/png':
             $gd = @imagecreatefrompng($temp_path);
             break;
         case 'image/gif':
             $gd = @imagecreatefromgif($temp_path);
             break;
         case 'image/bmp':
             $gd = @imagecreatefrombmp($temp_path);
             break;
         default:
             return null;
     }
     if (is_resource($gd)) {
         $width = imagesx($gd);
         $height = imagesy($gd);
         $hash = md5_file($temp_path);
         $list = Image::SELECT("Hash='" . Database::escape($hash) . "'");
         if (count($list)) {
             // La imagen ya existe :S
             $image = $list[0];
             $image->_setCounter($image->getCounter() + 1);
         } else {
             // Creo un nuevo registro de imagen :)
             $image = parent::INSERT();
             $image->_setWidth($width);
             $image->_setHeight($height);
             $image->_setMime($mime);
             $image->_setHash($hash);
             $image->_setSize(@filesize($temp_path));
             $image->_setCounter(1);
             // Copiar imagen a la carpeta de imágenes con el id de $image->getId(); (o con el hash)
             Rack::Write('img', md5($image->ID()), $temp_path);
         }
         Rack::Remove('temp', $temp_hash);
         return $image;
     } else {
         // Error al abrir la imagen
         Rack::Remove('temp', $temp_hash);
         return null;
     }
 }
Exemple #11
0
 private function init_img()
 {
     if ($this->type == 'jpeg') {
         $this->tem_file = imagecreatefromjpeg($this->sur_file);
     } elseif ($this->type == 'jpg') {
         $this->tem_file = imagecreatefromjpeg($this->sur_file);
     } elseif ($this->type == 'gif') {
         $this->tem_file = imagecreatefromgif($this->sur_file);
     } elseif ($this->type == 'png') {
         $this->tem_file = imagecreatefrompng($this->sur_file);
     } elseif ($this->type == 'bmp') {
         $this->tem_file = imagecreatefrombmp($this->sur_file);
     }
 }
Exemple #12
0
 public function dupeImage()
 {
     $raw_file = DB::get_item_collection_path($this->img_file);
     $file_path = Client::getFile($raw_file . '.bmp');
     if (file_get_contents($file_path)) {
         header('Content-type: image/png');
         $im = imagecreatefrombmp($file_path);
         //$purple = imagecolorallocate($im, 255, 0, 255);
         //imagecolortransparent($im,$purple);
         imagepng($im);
         imagedestroy($im);
     } else {
         echo "File does not exist!";
     }
 }
 private function createImage()
 {
     switch ($this->imagetype) {
         case 1:
             $image = imagecreatefromgif($this->filename);
             break;
         case 2:
             $image = imagecreatefromjpeg($this->filename);
             break;
         case 3:
             $image = imagecreatefrompng($this->filename);
             break;
         case 6:
             $image = imagecreatefrombmp($this->filename);
             break;
     }
     return $image;
 }
Exemple #14
0
 private function getImage()
 {
     $this->image_info = getimagesize($this->filename);
     $this->image_type = $this->image_info[2];
     switch ($this->image_type) {
         case IMAGETYPE_JPEG:
             $this->image = imagecreatefromjpeg($this->filename);
             break;
         case IMAGETYPE_GIF:
             $this->image = imagecreatefromgif($this->filename);
             break;
         case IMAGETYPE_PNG:
             $this->image = imagecreatefrompng($this->filename);
             break;
         case IMAGETYPE_BMP:
             $this->image = imagecreatefrombmp($this->filename);
             break;
     }
 }
function load_image($file)
{
    $ext = substr($file, strrpos($file, ".") + 1);
    $ext = strtolower($ext);
    $src_img = false;
    if ($ext == "jpg" || $ext == "jpeg") {
        $src_img = imagecreatefromjpeg($file);
    }
    if ($ext == "png") {
        $src_img = imagecreatefrompng($file);
    }
    if ($ext == "gif") {
        $src_img = imagecreatefromgif($file);
    }
    if ($ext == "bmp") {
        $src_img = imagecreatefrombmp($file);
    }
    return $src_img;
}
Exemple #16
0
 /**
  * Конвертация изображений с разным расширением в jpeg
  * 
  * @param string $source
  * @param string $target_directory
  * @param integer $quality
  * @param boolean $remove_source_image
  * @return boolean
  * @throws Kohana_Exception
  */
 public static function convert_to_jpeg($source, $target_directory = NULL, $quality = 100, $remove_source_image = FALSE)
 {
     if (!file_exists($source)) {
         return FALSE;
     }
     $ext = pathinfo($source, PATHINFO_EXTENSION);
     $filename = pathinfo($source, PATHINFO_FILENAME);
     if ($target_directory !== NULL) {
         if (!is_dir($target_directory) and is_writable($target_directory)) {
             mkdir($target_directory, 0777);
         }
     } else {
         $target_directory = pathinfo($source, PATHINFO_DIRNAME);
     }
     if (!is_writable($target_directory)) {
         throw new Kohana_Exception('Unable to write to the target directory :resource', array(':resource' => $target_directory));
     }
     if (Valid::regex($ext, '/jpg|jpeg/i')) {
         $image_tmp = imagecreatefromjpeg($source);
     } else {
         if (Valid::regex($ext, '/png/i')) {
             $image_tmp = imagecreatefrompng($source);
         } else {
             if (Valid::regex($ext, '/gif/i')) {
                 $image_tmp = imagecreatefromgif($source);
             } else {
                 if (Valid::regex($ext, '/bmp/i')) {
                     $image_tmp = imagecreatefrombmp($source);
                 } else {
                     return FALSE;
                 }
             }
         }
     }
     // quality is a value from 0 (worst) to 100 (best)
     imagejpeg($image_tmp, $dirname . $filename . '.jpg', $quality);
     imagedestroy($image_tmp);
     if ($remove_source_image === TRUE) {
         unlink($source);
     }
     return TRUE;
 }
Exemple #17
0
function mkthumb($orig, $thumb, $maxLength)
{
    $ext = strtolower(strrchr($orig, "."));
    //依照副檔名, 使用不同函式將原始照片載入記憶體
    switch ($ext) {
        case '.jpg':
            $picSrc = imagecreatefromjpeg($orig);
            break;
        case '.png':
            $picSrc = imagecreatefrompng($orig);
            break;
        case '.gif':
            $picSrc = imagecreatefromgif($orig);
            break;
        case '.bmp':
            $picSrc = imagecreatefrombmp($orig);
            break;
        default:
            //傳回錯誤訊息
            return "不支援 {$ext} 圖檔格式";
    }
    //取得原始圖的高度 ($picSrc_y) 與寬度 ($picSrc_x)
    $picSrc_x = imagesx($picSrc);
    $picSrc_y = imagesy($picSrc);
    //依照 $maxLength 參數, 計算縮圖應該使用的
    //高度 ($picDst_y) 與寬度 ($picDst_x)
    if ($picSrc_x > $picSrc_y) {
        $picDst_x = $maxLength;
        //intval() 可取得數字的整數部分
        $picDst_y = intval($picSrc_y / $picSrc_x * $maxLength);
    } else {
        $picDst_y = $maxLength;
        $picDst_x = intval($picSrc_x / $picSrc_y * $maxLength);
    }
    //在記憶體中建立新圖
    $picDst = imagecreatetruecolor($picDst_x, $picDst_y);
    //將原始照片複製並且縮小到新圖
    imagecopyresized($picDst, $picSrc, 0, 0, 0, 0, $picDst_x, $picDst_y, $picSrc_x, $picSrc_y);
    //將新圖寫入 $thumb 參數指定的縮圖檔名
    imagejpeg($picDst, $thumb);
    return 'ok';
}
function make_thumb($image_path, $image_name, $height, $width)
{
    // echo $image_path."-".$image_name."-".$height."-".$width;
    /* read the source image */
    $ext = pathinfo($image_name, PATHINFO_EXTENSION);
    if ($ext == 'jpg' || $ext == 'jpeg') {
        $source_image = imagecreatefromjpeg($image_name);
    } elseif ($ext == 'png') {
        $source_image = imagecreatefrompng($image_name);
    } elseif ($ext == 'gif') {
        $source_image = imagecreatefromgif($image_name);
    } elseif ($ext == 'bmp') {
        $source_image = imagecreatefrombmp($image_name);
    }
    $actual_width = imagesx($source_image);
    $actual_height = imagesy($source_image);
    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($width, $height);
    Imagefill($virtual_image, 0, 0, imagecolorallocate($virtual_image, 255, 255, 255));
    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $width, $height, $actual_width, $actual_height);
    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $image_path);
}
Exemple #19
0
function imgTableizr($imgSrc, $quality = 'medium', $width = 'no-resize')
{
    global $colspan, $prevHex, $prevRGB, $currentHex, $currentRGB, $tdCount, $output;
    $output = array();
    $explodedSrc = explode('.', $imgSrc);
    $ext = end($explodedSrc);
    switch ($ext) {
        case 'jpg' || 'jpeg':
            $img = imagecreatefromjpeg($imgSrc);
            break;
        case 'png':
            $img = imagecreatefrompng($imgSrc);
            break;
        case 'gif':
            $img = imagecreatefromgif($imgSrc);
            break;
        case 'bmp':
            $img = imagecreatefrombmp($imgSrc);
            break;
        default:
            return false;
    }
    if ($width != 'no-resize' && is_int($width) && $width > 0) {
        $img = imagescale($img, $width);
    }
    switch ($quality) {
        case 'medium':
            $threshold = 20;
            break;
        case 'low':
            $threshold = 30;
            break;
        case 'high':
            $threshold = 10;
            break;
        case 'maximum':
            $threshold = 0;
            break;
        case is_int($quality) && $quality >= 0:
            $threshold = $quality;
            break;
        case is_int($quality) && $quality < 0:
            $threshold = 0;
            break;
        default:
            return false;
    }
    $imgW = imagesx($img);
    $imgH = imagesy($img);
    array_push($output, '<table style="border-spacing:0;width:' . $imgW . 'px;height:' . $imgH . 'px;margin:0;padding:0;">');
    for ($y = 0; $imgH > $y; $y++) {
        array_push($output, '<tr>');
        for ($x = 0; $imgW > $x; $x++) {
            $rgb = imagecolorat($img, $x, $y);
            $currentRGB = array(0 => $rgb >> 16 & 0xff, 1 => $rgb >> 8 & 0xff, 2 => $rgb & 0xff);
            $currentHex = rgb2hex($currentRGB);
            if ($x != 0) {
                if (abs($currentRGB[0] - $prevRGB[0]) <= $threshold) {
                    if (abs($currentRGB[1] - $prevRGB[1]) <= $threshold) {
                        if (abs($currentRGB[2] - $prevRGB[2]) <= $threshold) {
                            $colspan++;
                            if ($x + 1 == $imgW) {
                                generateCell();
                            }
                        } else {
                            generateCell();
                        }
                    } else {
                        generateCell();
                    }
                } else {
                    generateCell();
                }
            } else {
                $prevHex = $currentHex;
                $prevRGB = $currentRGB;
            }
        }
        array_push($output, '</tr>');
    }
    array_push($output, '</table>');
    imagedestroy($img);
    echo implode('', $output);
}
Exemple #20
0
function resampleImage($path, $new_path, $sizew, $sizeh = false)
{
    if (false == $sizeh) {
        $sizeh = $sizew;
    }
    if (!isset($sizew) || $sizew < 50) {
        $sizew = 50;
    }
    if (!isset($sizeh) || $sizeh < 50) {
        $sizeh = 50;
    }
    $itype = 2;
    if (function_exists('exif_imagetype')) {
        $itype = exif_imagetype($path);
        switch ($itype) {
            case IMAGETYPE_JPEG:
                $img = imagecreatefromjpeg($path);
                break;
            case IMAGETYPE_GIF:
                $img = imagecreatefromgif($path);
                break;
            case IMAGETYPE_PNG:
                $img = imagecreatefrompng($path);
                break;
            case IMAGETYPE_BMP:
                $img = imagecreatefrombmp($path);
                break;
            default:
                return false;
        }
        if (!$img) {
            return false;
        }
    } else {
        if (function_exists('getimagesize')) {
            @($info = getimagesize($path));
            if (!$info || empty($info['mime'])) {
                return false;
            }
            $itype = $info['mime'];
            switch ($itype) {
                case 'image/jpeg':
                    $img = imagecreatefromjpeg($path);
                    break;
                case 'image/gif':
                    $img = imagecreatefromgif($path);
                    break;
                case 'image/png':
                    $img = imagecreatefrompng($path);
                    break;
                case 'image/bmp':
                    $img = imagecreatefrombmp($path);
                    break;
                default:
                    return false;
            }
        } else {
            $img = imagecreatefromjpeg($path);
        }
    }
    $w = imagesx($img);
    $h = imagesy($img);
    if ($w < $sizew && $h < $sizeh) {
        $nw = $w;
        $nh = $h;
    } else {
        if ($w / $sizew < $h / $sizeh) {
            $nw = intval($w * $sizeh / $h);
            $nh = $sizeh;
        } else {
            $nw = $sizew;
            $nh = intval($h * $sizew / $w);
        }
    }
    $dest = imagecreatetruecolor($nw, $nh);
    switch ($itype) {
        case 'image/gif':
        case 'image/png':
        case 3:
        case 1:
            imagecolortransparent($dest, imagecolortransparent($img));
            imagealphablending($dest, false);
            imagesavealpha($dest, true);
            break;
        default:
            break;
    }
    imagecopyresampled($dest, $img, 0, 0, 0, 0, $nw, $nh, $w, $h);
    $quality_jpeg = Config::read('quality_jpeg');
    if (isset($quality_jpeg)) {
        $quality_jpeg = intval($quality_jpeg) < 0 || intval($quality_jpeg) > 100 ? 75 : intval($quality_jpeg);
    }
    $quality_png = Config::read('quality_png');
    if (isset($quality_png)) {
        $quality_png = intval($quality_png) < 0 || intval($quality_png) > 9 ? 9 : intval($quality_png);
    }
    switch ($itype) {
        case 1:
        case 'image/gif':
            imagegif($dest, $new_path);
            break;
        case 2:
        case 'image/jpeg':
            imagejpeg($dest, $new_path, $quality_jpeg);
            break;
        case 3:
        case 'image/png':
            imagepng($dest, $new_path, $quality_png);
            break;
        default:
            imagejpeg($dest, $new_path, $quality_jpeg);
            break;
    }
    imagedestroy($img);
    imagedestroy($dest);
    return true;
}
 public function thumbsCreatorAction()
 {
     $filename = tempnam(sys_get_temp_dir(), 'picture_');
     $paramImg = $this->_getParam('img');
     $x = explode('/', $paramImg);
     $attachmentId = (int) $x[0];
     $db = Zend_Registry::get('dbAdapter');
     $sqlSelect = $db->select()->from('attachmentBlobs')->where('attachmentId = ?', $attachmentId);
     if ($row = $db->fetchRow($sqlSelect)) {
         file_put_contents($filename, $row['data']);
     }
     list($width, $height) = getimagesize($filename);
     $paramWidth = $this->_getParam('width');
     $paramHeight = $this->_getParam('height');
     $newWidth = $paramWidth ? $paramWidth : 0;
     $newHeight = $paramHeight ? $paramHeight : 0;
     $imgInfoList = getimagesize($filename);
     $widthRatio = $newWidth != 0 ? $width / $newWidth : 1;
     $heightRatio = $newHeight != 0 ? $height / $newHeight : 1;
     if ($widthRatio < 1 || $heightRatio < 1) {
         $widthRatio > $heightRatio ? $heightRatio = $widthRatio : ($widthRatio = $heightRatio);
         $newWidth = $newWidth < $width / $widthRatio ? $width / $widthRatio : $newWidth;
         $newHeight = $newHeight < $height / $heightRatio ? $height / $heightRatio : $newHeight;
     } else {
         $widthRatio > $heightRatio ? $heightRatio = $widthRatio : ($widthRatio = $heightRatio);
         $newWidth = $newWidth < $width / $widthRatio ? $width / $widthRatio : $newWidth;
         $newHeight = $newHeight < $height / $heightRatio ? $height / $heightRatio : $newHeight;
     }
     //Get file content type
     $mime = $imgInfoList['mime'];
     //Create img from file(url) based on img type
     switch ($mime) {
         case 'image/gif':
             $img = imagecreatefromgif($filename);
             break;
         case 'image/png':
             $img = imagecreatefrompng($filename);
             break;
         case 'image/jpeg':
             $img = imagecreatefromjpeg($filename);
             break;
         case 'image/bmp':
             $img = imagecreatefrombmp($filename);
             break;
     }
     $resizedImg = imagecreatetruecolor($newWidth, $newHeight);
     $bgColor = imagecolorallocate($resizedImg, 255, 255, 255);
     imagefill($resizedImg, 0, 0, $bgColor);
     if ($mime == 'image/gif' || $mime == 'image/png') {
         $colorTransparent = imagecolortransparent($img);
         imagecolortransparent($resizedImg, $colorTransparent);
     }
     // File and new size
     imagecopyresampled($resizedImg, $img, $newWidth > $width / $widthRatio ? ($newWidth - $width / $widthRatio) / 2 : 0, $newHeight > $height / $heightRatio ? ($newHeight - $height / $heightRatio) / 2 : 0, 0, 0, $width / $widthRatio, $height / $heightRatio, $width, $height);
     switch ($mime) {
         case 'image/gif':
             imagegif($resizedImg);
             break;
         case 'image/png':
             imagepng($resizedImg);
             break;
         case 'image/jpeg':
             imagejpeg($resizedImg);
             break;
         case 'image/bmp':
             imagetmp($resizedImg);
             break;
     }
     header('Content-type: ' . $mime);
     imagepng($resizedImg);
     imagedestroy($resizedImg);
     $this->render();
 }
Exemple #22
0
             mkdir($dir . $p, 0755);
         }
     }
     return $p;
 }
 private static function _init($file)
 {
     $info = getimagesize($file);
     $method = $im = $ext = null;
     switch ($info['mime']) {
         case 'image/jpeg':
         case 'image/jpg':
             $ext = '.jpg';
             $im = imagecreatefromjpeg($file);
             $method = 'imagejpeg';
             break;
         case 'image/gif':
             $ext = '.gif';
             $im = imagecreatefromgif($file);
             $method = 'imagegif';
             break;
         case 'image/png':
             $ext = '.png';
             $im = imagecreatefrompng($file);
             $method = 'imagepng';
             break;
         case 'image/bmp':
             $ext = '.bmp';
             $im = imagecreatefrombmp($file);
             $method = 'imagepng';
             break;
Exemple #23
0
 function createImage($type, $img_name)
 {
     switch ($type) {
         case 1:
             if (function_exists('imagecreatefromgif')) {
                 $tmp_img = @ImageCreateFromGIF($img_name);
             } else {
                 return false;
             }
             break;
         case 2:
             $tmp_img = ImageCreateFromJPEG($img_name);
             break;
         case 3:
             $tmp_img = ImageCreateFromPNG($img_name);
             break;
         case 6:
             $tmp_img = imagecreatefrombmp($img_name);
             break;
         default:
             $tmp_img = ImageCreateFromString($img_name);
             break;
     }
     return $tmp_img;
 }
Exemple #24
0
function check_and_move($filename)
{
    global $config, $_POST;
    $info = getimagesize($config['working_dir'] . $filename);
    $final_filename = '';
    switch ($info['mime']) {
        case 'image/gif':
            $ext = 'gif';
            break;
        case 'image/pjpeg':
            $ext = 'jpg';
            break;
        case 'image/jpeg':
            $ext = 'jpg';
            break;
        case 'image/x-png':
            $ext = 'png';
            break;
        case 'image/png':
            $ext = 'png';
            break;
        case 'image/bmp':
            $ext = 'bmp';
            break;
        case 'image/x-ms-bmp':
            $ext = 'bmp';
            break;
        default:
            $ext = 'fail';
            $info['mime'] = 'n/a';
            break;
    }
    $stat = stat($config['working_dir'] . $filename);
    $partes = explode('.', $filename);
    $extension = strtolower($partes[count($partes) - 1]);
    if ($info['mime'] == 'n/a') {
        $local_error[] = "Ошибка: Неверный MIME-тип изображения, допускаются " . implode(', ', $config['mimes']) . ". Вы пытались залить " . $info['mime'];
    } elseif (!in_array($ext, $config['extensions'])) {
        $local_error[] = "Ошибка: Неверное расширение изображения, допускаются " . strtoupper(implode(', ', $config['extensions'])) . ". Вы пытались залить " . strtoupper($extension);
    } elseif ($stat['size'] > $config['max_size_byte']) {
        $local_error[] = "Ошибка: Превышен максимальный размер файла: {$config['max_size_mb']} МБ";
    } elseif ($info['1'] > $config['max_height']) {
        $local_error[] = "Ошибка: Превышена максимальная высота изображения: {$config['max_height']} пикселей";
    } elseif ($info['0'] > $config['max_width']) {
        $local_error[] = "Ошибка: Превышена максимальная ширина изображения: {$config['max_width']} пикселей";
    }
    if (!isset($local_error)) {
        if ($ext == 'bmp') {
            include 'bmp.php';
            $src = imagecreatefrombmp($config['working_dir'] . $filename);
            imagepng($src, $config['working_dir'] . $filename);
            imagedestroy($src);
            $ext = 'png';
            $partes[count($partes) - 1] = $ext;
            $filenamepng = implode(".", $partes);
            if (!rename("{$config['working_dir']}{$filename}", "{$config['working_dir']}{$filenamepng}")) {
                $local_error[] = "Ошибка преобразования изображения";
            }
            $filename = $filenamepng;
            unset($filenamepng);
        }
        $final_filename = random_string($config['random_str_quantity'], 'lower,numbers') . "." . $ext;
        $uploaded_file_path = strtolower($config['uploaddir'] . $config['current_path'] . '/' . $final_filename);
        if (isset($_POST['thumb']) and $_POST['thumb'] == "true") {
            //если пользователь не выставил значение(я) превьюшки
            if ($_POST['thumb_width'] or $_POST['thumb_height']) {
                preview($filename, $final_filename, $_POST['thumb_width'], $_POST['thumb_height'], $config['quality']);
            } else {
                $local_error[] = "Ошибка: Не указан размер превью";
                unlink("{$config['working_dir']}{$filename}");
                exit;
            }
        }
        //если установлено уменьшение
        if (isset($_POST['resize']) and $_POST['resize'] == 'true') {
            if ($_POST['width'] or $_POST['height']) {
                resize("{$config['working_dir']}{$filename}", $_POST['width'], $_POST['height']);
            } else {
                $local_error[] = "Ошибка: Не указан размер уменьшенного рисунка";
                unlink("{$config['working_dir']}{$filename}");
                exit;
            }
        }
        if (!rename("{$config['working_dir']}{$filename}", "{$config['uploaddir']}{$config['current_path']}/{$final_filename}")) {
            $local_error[] = "Ошибка перемещения изображения";
        }
    } else {
        @unlink("{$config['working_dir']}{$filename}");
    }
    if (isset($local_error)) {
        $local_error_string = implode(', ', $local_error);
    } else {
        $local_error_string = '';
    }
    return array($final_filename, $local_error_string);
}
Exemple #25
0
 public function guild_emblem($gid)
 {
     $this->load->model('mcommunity');
     $ginfo = $this->mcommunity->guild_info(array('guild_id' => $gid));
     $emblem = $ginfo[0]->emblem_data;
     $emblem = @gzuncompress(pack('H*', $emblem));
     file_put_contents('emblem.bmp', $emblem);
     header('Content-type: image/png');
     $im = imagecreatefrombmp('emblem.bmp');
     $purple = imagecolorallocate($im, 255, 0, 255);
     imagecolortransparent($im, $purple);
     imagepng($im);
 }
 /**
  * @name 			create_mini_pic
  * @desc			生成微缩图
  * @author 			Ren Long
  * @date			2013-08-11
  * @return 			string
  */
 protected function create_mini_pic($image, $name = '')
 {
     header("Content-Type: text/html;charset=utf-8");
     //        $mini_width = 181;
     $mini_height = 300;
     $im_info = getimagesize($image);
     switch ($im_info[2]) {
         case 1:
             $src_image = imagecreatefromgif($image);
             break;
         case 2:
             $src_image = imagecreatefromjpeg($image);
             break;
         case 3:
             $src_image = imagecreatefrompng($image);
             break;
         case 6:
             $src_image = imagecreatefrombmp($image);
             break;
     }
     $temp = $mini_height / $im_info[1];
     $new_w = $im_info[0] * $temp;
     $new_h = $mini_height;
     //生成文字水印
     //             $mark = imagecolorallocate($mini_image);
     //             $text = iconv("gbk", "utf-8", "renlong.ouzhe.com");
     //             imagettftext($mini_image, 12, 0, 20, 20, $mark, '黑体', $text);
     $mini_image = imagecreatetruecolor($new_w, $new_h);
     //        $mini_bg = imagecolorallocate( $mini_image , 255 , 0 , 0 );
     //        imagefill($mini_image, 0, 0, $mini_bg);
     imagecopyresized($mini_image, $src_image, 0, 0, 0, 0, $new_w, $new_h, $im_info[0], $im_info[1]);
     imagepng($mini_image, "images/pic/mini/" . $name . ".png");
     $image_mini_add = "images/pic/mini/" . $name . ".png";
     return $image_mini_add;
 }
Exemple #27
0
}
global $CONTEXTUSER;
$CONTEXTUSER = $user;
$in = $user->photo;
$imagetype = exif_imagetype($in);
if ($imagetype == IMAGETYPE_JPEG) {
    $image = @imagecreatefromjpeg($in);
} else {
    if ($imagetype == IMAGETYPE_GIF) {
        $image = @imagecreatefromgif($in);
    } else {
        if ($imagetype == IMAGETYPE_PNG) {
            $image = @imagecreatefrompng($in);
        } else {
            if ($imagetype == IMAGETYPE_BMP) {
                $image = @imagecreatefrombmp($in);
            }
        }
    }
}
if (isset($image)) {
    $imagewidth = imagesx($image);
    $imageheight = imagesy($image);
}
?>
<script type="text/javascript">

function getUserFeeds() {
	args = new Object();
	args['userid'] = '<?php 
echo $user->userid;
Exemple #28
0
$originalFileName = "images/originals/{$image_id}.{$file_extension}";
$smallFileName = "images/thumbs_small/{$image_id}.{$file_extension}";
$mediumFileName = "images/thumbs_medium/{$image_id}.{$file_extension}";
move_uploaded_file($_FILES['image']['tmp_name'], $originalFileName);
switch ($image_type) {
    case IMAGETYPE_GIF:
        $original = imagecreatefromgif($originalFileName);
        break;
    case IMAGETYPE_JPEG:
        $original = imagecreatefromjpeg($originalFileName);
        break;
    case IMAGETYPE_PNG:
        $original = imagecreatefrompng($originalFileName);
        break;
    case IMAGETYPE_BMP:
        $original = imagecreatefrombmp($originalFileName);
        break;
    default:
        break;
}
// $original = imagecreatefromjpeg($originalFileName);
$width = imagesx($original);
$height = imagesy($original);
$square = min($width, $height);
// Create small square thumbnail
$small = imagecreatetruecolor(200, 200);
imagecopyresized($small, $original, 0, 0, $width > $square ? ($width - $square) / 2 : 0, $height > $square ? ($height - $square) / 2 : 0, 200, 200, $square, $square);
//imagejpeg($small, $smallFileName);
switch ($image_type) {
    case IMAGETYPE_GIF:
        imagegif($small, $smallFileName);
Exemple #29
0
 function resize($size, $x = 0, $y = 0, $w = null, $h = null)
 {
     $w = $w === null ? $this->width : $w;
     $h = $h === null ? $this->height : $h;
     if (!file_exists($this->filepath)) {
         throw new Exception(_('Lost our file.'));
         return;
     }
     // Don't crop/scale if it isn't necessary
     if ($size === $this->width && $size === $this->height && $x === 0 && $y === 0 && $w === $this->width && $h === $this->height) {
         $outname = Avatar::filename($this->id, image_type_to_extension($this->type), $size, common_timestamp());
         $outpath = Avatar::path($outname);
         @copy($this->filepath, $outpath);
         return $outname;
     }
     switch ($this->type) {
         case IMAGETYPE_GIF:
             $image_src = imagecreatefromgif($this->filepath);
             break;
         case IMAGETYPE_JPEG:
             $image_src = imagecreatefromjpeg($this->filepath);
             break;
         case IMAGETYPE_PNG:
             $image_src = imagecreatefrompng($this->filepath);
             break;
         case IMAGETYPE_BMP:
             $image_src = imagecreatefrombmp($this->filepath);
             break;
         case IMAGETYPE_WBMP:
             $image_src = imagecreatefromwbmp($this->filepath);
             break;
         case IMAGETYPE_XBM:
             $image_src = imagecreatefromxbm($this->filepath);
             break;
         default:
             throw new Exception(_('Unknown file type'));
             return;
     }
     $image_dest = imagecreatetruecolor($size, $size);
     if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG || $this->type == IMAGETYPE_BMP) {
         $transparent_idx = imagecolortransparent($image_src);
         if ($transparent_idx >= 0) {
             $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
             $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
             imagefill($image_dest, 0, 0, $transparent_idx);
             imagecolortransparent($image_dest, $transparent_idx);
         } elseif ($this->type == IMAGETYPE_PNG) {
             imagealphablending($image_dest, false);
             $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
             imagefill($image_dest, 0, 0, $transparent);
             imagesavealpha($image_dest, true);
         }
     }
     imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
     if ($this->type == IMAGETYPE_BMP) {
         //we don't want to save BMP... it's an inefficient, rare, antiquated format
         //save png instead
         $this->type = IMAGETYPE_PNG;
     } else {
         if ($this->type == IMAGETYPE_WBMP) {
             //we don't want to save WBMP... it's a rare format that we can't guarantee clients will support
             //save png instead
             $this->type = IMAGETYPE_PNG;
         } else {
             if ($this->type == IMAGETYPE_XBM) {
                 //we don't want to save XBM... it's a rare format that we can't guarantee clients will support
                 //save png instead
                 $this->type = IMAGETYPE_PNG;
             }
         }
     }
     $outname = Avatar::filename($this->id, image_type_to_extension($this->type), $size, common_timestamp());
     $outpath = Avatar::path($outname);
     switch ($this->type) {
         case IMAGETYPE_GIF:
             imagegif($image_dest, $outpath);
             break;
         case IMAGETYPE_JPEG:
             imagejpeg($image_dest, $outpath, 100);
             break;
         case IMAGETYPE_PNG:
             imagepng($image_dest, $outpath);
             break;
         default:
             throw new Exception(_('Unknown file type'));
             return;
     }
     imagedestroy($image_src);
     imagedestroy($image_dest);
     return $outname;
 }
 /**
  * Render a background image over a rectangular area
  *
  * @param string $url      The background image to load
  * @param float  $x        The left edge of the rectangular area
  * @param float  $y        The top edge of the rectangular area
  * @param float  $width    The width of the rectangular area
  * @param float  $height   The height of the rectangular area
  * @param Style  $style    The associated Style object
  *
  * @throws Exception
  */
 protected function _background_image($url, $x, $y, $width, $height, $style)
 {
     if (!function_exists("imagecreatetruecolor")) {
         throw new Exception("The PHP GD extension is required, but is not installed.");
     }
     $sheet = $style->get_stylesheet();
     // Skip degenerate cases
     if ($width == 0 || $height == 0) {
         return;
     }
     $box_width = $width;
     $box_height = $height;
     //debugpng
     if (DEBUGPNG) {
         print '[_background_image ' . $url . ']';
     }
     list($img, $type, ) = Image_Cache::resolve_url($url, $sheet->get_protocol(), $sheet->get_host(), $sheet->get_base_path(), $this->_dompdf);
     // Bail if the image is no good
     if (Image_Cache::is_broken($img)) {
         return;
     }
     //Try to optimize away reading and composing of same background multiple times
     //Postponing read with imagecreatefrom   ...()
     //final composition parameters and name not known yet
     //Therefore read dimension directly from file, instead of creating gd object first.
     //$img_w = imagesx($src); $img_h = imagesy($src);
     list($img_w, $img_h) = dompdf_getimagesize($img);
     if (!isset($img_w) || $img_w == 0 || !isset($img_h) || $img_h == 0) {
         return;
     }
     $repeat = $style->background_repeat;
     $dpi = $this->_dompdf->get_option("dpi");
     //Increase background resolution and dependent box size according to image resolution to be placed in
     //Then image can be copied in without resize
     $bg_width = round((double) ($width * $dpi) / 72);
     $bg_height = round((double) ($height * $dpi) / 72);
     //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel
     list($bg_x, $bg_y) = $style->background_position;
     if (is_percent($bg_x)) {
         // The point $bg_x % from the left edge of the image is placed
         // $bg_x % from the left edge of the background rectangle
         $p = (double) $bg_x / 100.0;
         $x1 = $p * $img_w;
         $x2 = $p * $bg_width;
         $bg_x = $x2 - $x1;
     } else {
         $bg_x = (double) ($style->length_in_pt($bg_x) * $dpi) / 72;
     }
     $bg_x = round($bg_x + $style->length_in_pt($style->border_left_width) * $dpi / 72);
     if (is_percent($bg_y)) {
         // The point $bg_y % from the left edge of the image is placed
         // $bg_y % from the left edge of the background rectangle
         $p = (double) $bg_y / 100.0;
         $y1 = $p * $img_h;
         $y2 = $p * $bg_height;
         $bg_y = $y2 - $y1;
     } else {
         $bg_y = (double) ($style->length_in_pt($bg_y) * $dpi) / 72;
     }
     $bg_y = round($bg_y + $style->length_in_pt($style->border_top_width) * $dpi / 72);
     //clip background to the image area on partial repeat. Nothing to do if img off area
     //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area
     //On no repeat with positive offset: move size/start to have offset==0
     //Handle x/y Dimensions separately
     if ($repeat !== "repeat" && $repeat !== "repeat-x") {
         //No repeat x
         if ($bg_x < 0) {
             $bg_width = $img_w + $bg_x;
         } else {
             $x += $bg_x * 72 / $dpi;
             $bg_width = $bg_width - $bg_x;
             if ($bg_width > $img_w) {
                 $bg_width = $img_w;
             }
             $bg_x = 0;
         }
         if ($bg_width <= 0) {
             return;
         }
         $width = (double) ($bg_width * 72) / $dpi;
     } else {
         //repeat x
         if ($bg_x < 0) {
             $bg_x = -(-$bg_x % $img_w);
         } else {
             $bg_x = $bg_x % $img_w;
             if ($bg_x > 0) {
                 $bg_x -= $img_w;
             }
         }
     }
     if ($repeat !== "repeat" && $repeat !== "repeat-y") {
         //no repeat y
         if ($bg_y < 0) {
             $bg_height = $img_h + $bg_y;
         } else {
             $y += $bg_y * 72 / $dpi;
             $bg_height = $bg_height - $bg_y;
             if ($bg_height > $img_h) {
                 $bg_height = $img_h;
             }
             $bg_y = 0;
         }
         if ($bg_height <= 0) {
             return;
         }
         $height = (double) ($bg_height * 72) / $dpi;
     } else {
         //repeat y
         if ($bg_y < 0) {
             $bg_y = -(-$bg_y % $img_h);
         } else {
             $bg_y = $bg_y % $img_h;
             if ($bg_y > 0) {
                 $bg_y -= $img_h;
             }
         }
     }
     //Optimization, if repeat has no effect
     if ($repeat === "repeat" && $bg_y <= 0 && $img_h + $bg_y >= $bg_height) {
         $repeat = "repeat-x";
     }
     if ($repeat === "repeat" && $bg_x <= 0 && $img_w + $bg_x >= $bg_width) {
         $repeat = "repeat-y";
     }
     if ($repeat === "repeat-x" && $bg_x <= 0 && $img_w + $bg_x >= $bg_width || $repeat === "repeat-y" && $bg_y <= 0 && $img_h + $bg_y >= $bg_height) {
         $repeat = "no-repeat";
     }
     //Use filename as indicator only
     //different names for different variants to have different copies in the pdf
     //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color)
     //Note: Here, bg_* are the start values, not end values after going through the tile loops!
     $filedummy = $img;
     $is_png = false;
     $filedummy .= '_' . $bg_width . '_' . $bg_height . '_' . $bg_x . '_' . $bg_y . '_' . $repeat;
     //Optimization to avoid multiple times rendering the same image.
     //If check functions are existing and identical image already cached,
     //then skip creation of duplicate, because it is not needed by addImagePng
     if ($this->_canvas instanceof CPDF_Adapter && $this->_canvas->get_cpdf()->image_iscached($filedummy)) {
         $bg = null;
     } else {
         // Create a new image to fit over the background rectangle
         $bg = imagecreatetruecolor($bg_width, $bg_height);
         switch (strtolower($type)) {
             case IMAGETYPE_PNG:
                 $is_png = true;
                 imagesavealpha($bg, true);
                 imagealphablending($bg, false);
                 $src = imagecreatefrompng($img);
                 break;
             case IMAGETYPE_JPEG:
                 $src = imagecreatefromjpeg($img);
                 break;
             case IMAGETYPE_GIF:
                 $src = imagecreatefromgif($img);
                 break;
             case IMAGETYPE_BMP:
                 $src = imagecreatefrombmp($img);
                 break;
             default:
                 return;
                 // Unsupported image type
         }
         if ($src == null) {
             return;
         }
         //Background color if box is not relevant here
         //Non transparent image: box clipped to real size. Background non relevant.
         //Transparent image: The image controls the transparency and lets shine through whatever background.
         //However on transparent image preset the composed image with the transparency color,
         //to keep the transparency when copying over the non transparent parts of the tiles.
         $ti = imagecolortransparent($src);
         if ($ti >= 0) {
             $tc = imagecolorsforindex($src, $ti);
             $ti = imagecolorallocate($bg, $tc['red'], $tc['green'], $tc['blue']);
             imagefill($bg, 0, 0, $ti);
             imagecolortransparent($bg, $ti);
         }
         //This has only an effect for the non repeatable dimension.
         //compute start of src and dest coordinates of the single copy
         if ($bg_x < 0) {
             $dst_x = 0;
             $src_x = -$bg_x;
         } else {
             $src_x = 0;
             $dst_x = $bg_x;
         }
         if ($bg_y < 0) {
             $dst_y = 0;
             $src_y = -$bg_y;
         } else {
             $src_y = 0;
             $dst_y = $bg_y;
         }
         //For historical reasons exchange meanings of variables:
         //start_* will be the start values, while bg_* will be the temporary start values in the loops
         $start_x = $bg_x;
         $start_y = $bg_y;
         // Copy regions from the source image to the background
         if ($repeat === "no-repeat") {
             // Simply place the image on the background
             imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
         } else {
             if ($repeat === "repeat-x") {
                 for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
                     if ($bg_x < 0) {
                         $dst_x = 0;
                         $src_x = -$bg_x;
                         $w = $img_w + $bg_x;
                     } else {
                         $dst_x = $bg_x;
                         $src_x = 0;
                         $w = $img_w;
                     }
                     imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
                 }
             } else {
                 if ($repeat === "repeat-y") {
                     for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
                         if ($bg_y < 0) {
                             $dst_y = 0;
                             $src_y = -$bg_y;
                             $h = $img_h + $bg_y;
                         } else {
                             $dst_y = $bg_y;
                             $src_y = 0;
                             $h = $img_h;
                         }
                         imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
                     }
                 } else {
                     if ($repeat === "repeat") {
                         for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
                             for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
                                 if ($bg_x < 0) {
                                     $dst_x = 0;
                                     $src_x = -$bg_x;
                                     $w = $img_w + $bg_x;
                                 } else {
                                     $dst_x = $bg_x;
                                     $src_x = 0;
                                     $w = $img_w;
                                 }
                                 if ($bg_y < 0) {
                                     $dst_y = 0;
                                     $src_y = -$bg_y;
                                     $h = $img_h + $bg_y;
                                 } else {
                                     $dst_y = $bg_y;
                                     $src_y = 0;
                                     $h = $img_h;
                                 }
                                 imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
                             }
                         }
                     } else {
                         print 'Unknown repeat!';
                     }
                 }
             }
         }
         imagedestroy($src);
     }
     /* End optimize away creation of duplicates */
     $this->_canvas->clipping_rectangle($x, $y, $box_width, $box_height);
     //img: image url string
     //img_w, img_h: original image size in px
     //width, height: box size in pt
     //bg_width, bg_height: box size in px
     //x, y: left/top edge of box on page in pt
     //start_x, start_y: placement of image relative to pattern
     //$repeat: repeat mode
     //$bg: GD object of result image
     //$src: GD object of original image
     //When using cpdf and optimization to direct png creation from gd object is available,
     //don't create temp file, but place gd object directly into the pdf
     if (!$is_png && $this->_canvas instanceof CPDF_Adapter) {
         // Note: CPDF_Adapter image converts y position
         $this->_canvas->get_cpdf()->addImagePng($filedummy, $x, $this->_canvas->get_height() - $y - $height, $width, $height, $bg);
     } else {
         $tmp_dir = $this->_dompdf->get_option("temp_dir");
         $tmp_name = tempnam($tmp_dir, "bg_dompdf_img_");
         @unlink($tmp_name);
         $tmp_file = "{$tmp_name}.png";
         //debugpng
         if (DEBUGPNG) {
             print '[_background_image ' . $tmp_file . ']';
         }
         imagepng($bg, $tmp_file);
         $this->_canvas->image($tmp_file, $x, $y, $width, $height);
         imagedestroy($bg);
         //debugpng
         if (DEBUGPNG) {
             print '[_background_image unlink ' . $tmp_file . ']';
         }
         if (!DEBUGKEEPTEMP) {
             unlink($tmp_file);
         }
     }
     $this->_canvas->clipping_end();
 }