/** Returns an array. Element 0 - GD resource. Element 1 - width. Element 2 - height. * Returns FALSE on failure. The only one parameter $image can be an instance of this class, * a GD resource, an array(width, height) or path to image file. * @param mixed $image * @return array */ protected function build_image($image) { if ($image instanceof gd) { $width = $image->get_width(); $height = $image->get_height(); $image = $image->get_image(); } elseif (is_resource($image) && get_resource_type($image) == "gd") { $width = @imagesx($image); $height = @imagesy($image); } elseif (is_array($image)) { list($key, $width) = each($image); list($key, $height) = each($image); $image = imagecreatetruecolor($width, $height); } elseif (false !== (list($width, $height, $type) = @getimagesize($image))) { $image = $type == IMAGETYPE_GIF ? @imagecreatefromgif($image) : ($type == IMAGETYPE_WBMP ? @imagecreatefromwbmp($image) : ($type == IMAGETYPE_JPEG ? @imagecreatefromjpeg($image) : ($type == IMAGETYPE_JPEG2000 ? @imagecreatefromjpeg($image) : ($type == IMAGETYPE_PNG ? imagecreatefrompng($image) : ($type == IMAGETYPE_XBM ? @imagecreatefromxbm($image) : false))))); if ($type == IMAGETYPE_PNG) { imagealphablending($image, false); } } $return = is_resource($image) && get_resource_type($image) == "gd" && isset($width) && isset($height) && preg_match('/^[1-9][0-9]*$/', $width) !== false && preg_match('/^[1-9][0-9]*$/', $height) !== false ? array($image, $width, $height) : false; if ($return !== false && isset($type)) { $this->type = $type; } return $return; }
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; }
public function input($input) { // if is not a string, not an existing file or not a file ressource if (!is_string($input) || !file_exists($input) || !is_file($input)) { throw new \InvalidArgumentException('Input file is not a valid ressource.'); } $this->inputPath = $input; list($this->inputWidth, $this->inputHeight, $this->inputType) = getimagesize($input); switch ($this->inputType) { case IMAGETYPE_GIF: if (!function_exists('imagecreatefromgif')) { throw new CapabilityException('GIF is not supported.'); } $this->input = imagecreatefromgif($input); break; case IMAGETYPE_JPEG: if (!function_exists('imagecreatefromjpeg')) { throw new CapabilityException('JPEG is not supported.'); } $this->input = imagecreatefromjpeg($input); break; case IMAGETYPE_PNG: if (!function_exists('imagecreatefrompng')) { throw new CapabilityException('PNG is not supported.'); } $this->input = imagecreatefrompng($input); break; case IMAGETYPE_WBMP: if (!function_exists('imagecreatefromwbmp')) { throw new CapabilityException('WBMP is not supported.'); } $this->input = imagecreatefromwbmp($input); break; // Not supported yet in PHP 5.5. WebP is supported since in PHP 5.5 (https://bugs.php.net/bug.php?id=65038) // Not supported yet in PHP 5.5. WebP is supported since in PHP 5.5 (https://bugs.php.net/bug.php?id=65038) case defined('IMAGETYPE_WEBP') && IMAGETYPE_WEBP: if (!function_exists('imagecreatefromwebp')) { throw new CapabilityException('WebP is not supported.'); } $this->input = imagecreatefromwebp($input); break; case IMAGETYPE_XBM: if (!function_exists('imagecreatefromxbm')) { throw new CapabilityException('XBM is not supported.'); } $this->input = imagecreatefromxbm($input); break; case defined('IMAGETYPE_WEBP') && IMAGETYPE_XPM: if (!function_exists('imagecreatefromxpm')) { throw new CapabilityException('XPM is not supported.'); } $this->input = imagecreatefromxpm($input); break; default: throw new CapabilityException('Unsupported input file type.'); break; } $this->applyExifTransformations($this->input); }
/** * Read image * * @param string uri * @return resource * @throws img.ImagingException */ public function readImageFromUri($uri) { if (FALSE === ($r = imagecreatefromxbm($uri))) { $e = new ImagingException('Cannot read image from "' . $uri . '"'); xp::gc(__FILE__); throw $e; } return $r; }
/** * Read image via imagecreatefromxbm() * * @param string uri * @return resource * @throws img.ImagingException */ protected function readImage0($uri) { if (FALSE === ($r = imagecreatefromxbm($uri))) { $e = new ImagingException('Cannot read image'); xp::gc(__FILE__); throw $e; } return $r; }
public function __construct($file, $width = null, $height = null) { if (!self::$_checked) { self::check(); } $this->_file = $file; if (file_exists($this->_file)) { $this->_realpath = realpath($this->_file); $imageinfo = getimagesize($this->_file); if ($imageinfo) { $this->_width = $imageinfo[0]; $this->_height = $imageinfo[1]; $this->_type = $imageinfo[2]; $this->_mime = $imageinfo["mime"]; } switch ($this->_type) { case 1: $this->_image = imagecreatefromgif($this->_file); break; case 2: $this->_image = imagecreatefromjpeg($this->_file); break; case 3: $this->_image = imagecreatefrompng($this->_file); break; case 15: $this->_image = imagecreatefromwbmp($this->_file); break; case 16: $this->_image = imagecreatefromxbm($this->_file); break; default: if ($this->_mime) { throw new Exception("Installed GD does not support " . $this->_mime . " images"); } else { throw new Exception("Installed GD does not support such images"); } break; } imagesavealpha($this->_image, true); } else { if (!$width || !$height) { throw new Exception("Failed to create image from file " . $this->_file); } $this->_image = imagecreatetruecolor($width, $height); imagealphablending($this->_image, true); imagesavealpha($this->_image, true); $this->_realpath = $this->_file; $this->_width = $width; $this->_height = $height; $this->_type = 3; $this->_mime = "image/png"; } }
function image_resize($upfile, $output_filename, $output_path, $dst_w = 100, $dst_h = 100, $isRadio = 1, $trans_color = array(254, 254, 254)) { $imagedata = GetImageSize($upfile); // Read the size $src_w = $imagedata[0]; $src_h = $imagedata[1]; $src_type = $imagedata[2]; $re_dst_x = 0; $re_dst_y = 0; if ($isRadio | 0 > 0) { if ($dst_w >= $src_w && $dst_h >= $src_h) { $re_dst_w = $src_w; $re_dst_h = $src_h; } else { $p_w = $dst_w / $src_w; $p_h = $dst_h / $src_h; $p = min($p_w, $p_h); $re_dst_w = $src_w * $p; $re_dst_h = $src_h * $p; } } else { $re_dst_w = $dst_w; $re_dst_h = $dst_h; } if ($src_type == 1) { $src_image = ImageCreateFromGif($upfile); } else { if ($src_type == 2) { $src_image = ImageCreateFromJpeg($upfile); } else { if ($src_type == 3) { $src_image = ImageCreateFromPng($upfile); } else { if ($src_type == 16) { $src_image = imagecreatefromxbm($upfile); } } } } //else if ($src_type==6) // return; $dst_image = imagecreatetruecolor($re_dst_w, $re_dst_h); $bgc = imagecolorallocate($dst_image, $trans_color[0], $trans_color[1], $trans_color[2]); imagefilledrectangle($dst_image, 0, 0, $re_dst_w, $re_dst_h, $bgc); imagecolortransparent($dst_image, $bgc); imagecopyresampled($dst_image, $src_image, $re_dst_x, $re_dst_y, 0, 0, $re_dst_w, $re_dst_h, $src_w, $src_h); imagepng($dst_image, $output_path . $output_filename); }
function __construct($file_name) { $this->file_name = $file_name; if (is_null(self::$load_image_funcs)) { self::$load_image_funcs = array(IMAGETYPE_GIF => function ($fn) { return imagecreatefromgif($fn); }, IMAGETYPE_JPEG => function ($fn) { return imagecreatefromjpeg($fn); }, IMAGETYPE_PNG => function ($fn) { return imagecreatefrompng($fn); }, IMAGETYPE_WBMP => function ($fn) { imagecreatefromwbmp($fn); }, IMAGETYPE_XBM => function ($fn) { imagecreatefromxbm($fn); }); } }
function _load($img) { if (!function_exists('imagecreate')) { return false; } $dims = getimagesize($img); if (empty($dims)) { return false; } $this->OWIDTH = $this->WIDTH = $dims[0]; $this->OHEIGHT = $this->HEIGHT = $dims[1]; $this->FILENAME = $img; $types = array(IMAGETYPE_GIF => 'GIF', IMAGETYPE_JPEG => 'JPEG', IMAGETYPE_PNG => 'PNG', IMAGETYPE_WBMP => 'WBMP', IMAGETYPE_XBM => 'XBM'); if (!isset($types[$dims[2]])) { return false; } // unsupported type? TODO: maybe change this $this->TYPE = $types[$dims[2]]; $this->typeGD = $dims[2]; if ($this->_img) { imagedestroy($this->_img); } switch ($dims[2]) { case IMAGETYPE_PNG: $this->_img = imagecreatefrompng($img); break; case IMAGETYPE_JPEG: $this->_img = imagecreatefromjpeg($img); break; case IMAGETYPE_GIF: $this->_img = imagecreatefromgif($img); break; case IMAGETYPE_WBMP: $this->_img = imagecreatefromwbmp($img); break; case IMAGETYPE_XBM: $this->_img = imagecreatefromxbm($img); break; } if (!$this->_img) { return false; } return $this; }
/** * Create thumnbnail and return it's URL on success * * @param string $path file path * @param string $mime file mime type * @return string|false * @author Dmitry (dio) Levashov **/ protected function createTmb($path) { $mime = $this->mimetype($path); if (!$this->canCreateTmb($path, $mime)) { return false; } $name = $this->tmbName($path); $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name; // copy image in tmbPath so some drivers does not store files on local fs if (($src = $this->_fopen($path, 'rb')) == false || ($trg = @fopen($tmb, 'wb')) == false) { return false; } while (!feof($src)) { fwrite($trg, fread($src, 8192)); } $this->_fclose($src, $path); fclose($trg); if (($s = @getimagesize($tmb)) == false) { return false; } $result = false; $tmbSize = $this->tmbSize; list($x, $y, $size_w, $size_h) = $this->tmbEffects($s[0], $s[1], $tmbSize, $this->options['tmbCrop']); switch ($this->imgLib) { case 'imagick': try { $img = new imagick($tmb); } catch (Exception $e) { return false; } $img->contrastImage(1); if ($this->options['tmbCrop'] == false) { $img1 = new Imagick(); $img1->newImage($tmbSize, $tmbSize, new ImagickPixel($this->options['tmbBgColor'])); $img1->setImageFormat('png'); $img->resizeImage($size_w, $size_h, NULL, true); $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y); $result = $img1->writeImage($tmb); } else { $result = $img->cropThumbnailImage($tmbSize, $tmbSize) && $img->writeImage($tmb); } break; case 'gd': if ($s['mime'] == 'image/jpeg') { $img = imagecreatefromjpeg($tmb); } elseif ($s['mime'] == 'image/png') { $img = imagecreatefrompng($tmb); } elseif ($s['mime'] == 'image/gif') { $img = imagecreatefromgif($tmb); } elseif ($s['mime'] == 'image/xbm') { $img = imagecreatefromxbm($tmb); } if ($img && false != ($tmp = imagecreatetruecolor($tmbSize, $tmbSize))) { if ($this->options['tmbCrop'] == false) { if ($this->options['tmbBgColor'] == 'transparent') { list($r, $g, $b) = array(0, 0, 255); } else { list($r, $g, $b) = sscanf($this->options['tmbBgColor'], "#%02x%02x%02x"); } $bgcolor = imagecolorallocate($tmp, $r, $g, $b); if ($this->options['tmbBgColor'] == 'transparent') { $bgcolor = imagecolortransparent($tmp, $bgcolor); } imagefill($tmp, 0, 0, $bgcolor); if (!imagecopyresampled($tmp, $img, $x, $y, 0, 0, $size_w, $size_h, $s[0], $s[1])) { return false; } } else { if (!imagecopyresampled($tmp, $img, 0, 0, $x, $y, $tmbSize, $tmbSize, $size_w, $size_h)) { return false; } } $result = imagepng($tmp, $tmb, 7); imagedestroy($img); imagedestroy($tmp); } break; } return $result ? $name : false; }
protected function _getResource($data, $path) { //if the GD Library is not installed if (!function_exists('gd_info')) { //throw error Eden_Image_Error::i(Eden_Image_Error::GD_NOT_INSTALLED)->trigger(); } # imagecreatefromgd — Create a new image from GD file or URL # imagecreatefromgif — Create a new image from file or URL # imagecreatefromjpeg — Create a new image from file or URL # imagecreatefrompng — Create a new image from file or URL # imagecreatefromstring — Create a new image from the image stream in the string # imagecreatefromwbmp — Create a new image from file or URL # imagecreatefromxbm — Create a new image from file or URL # imagecreatefromxpm — Create a new image from file or URL $resource = false; if (!$path) { return imagecreatefromstring($data); } //depending on the extension lets load //the file using the right GD loader switch ($this->_type) { case 'gd': $resource = imagecreatefromgd($data); break; case 'gif': $resource = imagecreatefromgif($data); break; case 'jpg': case 'jpeg': case 'pjpeg': $resource = imagecreatefromjpeg($data); break; case 'png': $resource = imagecreatefrompng($data); break; case 'bmp': case 'wbmp': $resource = imagecreatefromwbmp($data); break; case 'xbm': $resource = imagecreatefromxbm($data); break; case 'xpm': $resource = imagecreatefromxpm($data); break; } //if there is no resource still if (!$resource) { //throw error Eden_Image_Error::i()->setMessage(Eden_Image_Error::NOT_VALID_IMAGE_FILE)->addVariable($path); } return $resource; }
public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $watermarkImageOpacity = 30, $repeat = false) { list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($watermarkImage); $this->_getFileAttributes(); switch ($watermarkFileType) { case IMAGETYPE_GIF: $watermark = imagecreatefromgif($watermarkImage); break; case IMAGETYPE_JPEG: $watermark = imagecreatefromjpeg($watermarkImage); break; case IMAGETYPE_PNG: $watermark = imagecreatefrompng($watermarkImage); break; case IMAGETYPE_XBM: $watermark = imagecreatefromxbm($watermarkImage); break; case IMAGETYPE_WBMP: $watermark = imagecreatefromxbm($watermarkImage); break; default: throw new Exception("Unsupported watermark image format."); break; } if ($this->getWatermarkWidth() && $this->getWatermarkHeigth() && $this->getWatermarkPosition() != self::POSITION_STRETCH) { $newWatermark = imagecreatetruecolor($this->getWatermarkWidth(), $this->getWatermarkHeigth()); imagealphablending($newWatermark, false); $col = imagecolorallocate($newWatermark, 255, 255, 255); imagefilledrectangle($newWatermark, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), $col); imagealphablending($newWatermark, true); imagecopyresampled($newWatermark, $watermark, 0, 0, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), imagesx($watermark), imagesy($watermark)); $watermark = $newWatermark; } if ($this->getWatermarkPosition() == self::POSITION_TILE) { $repeat = true; } elseif ($this->getWatermarkPosition() == self::POSITION_STRETCH) { $newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight); imagealphablending($newWatermark, false); $col = imagecolorallocate($newWatermark, 255, 255, 255); imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col); imagealphablending($newWatermark, true); imagecopyresampled($newWatermark, $watermark, 0, 0, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, imagesx($watermark), imagesy($watermark)); $watermark = $newWatermark; } elseif ($this->getWatermarkPosition() == self::POSITION_TOP_RIGHT) { $positionX = $this->_imageSrcWidth - imagesx($watermark); imagecopymerge($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity); } elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT) { $positionX = $this->_imageSrcWidth - imagesx($watermark); $positionY = $this->_imageSrcHeight - imagesy($watermark); imagecopymerge($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity); } elseif ($this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT) { $positionY = $this->_imageSrcHeight - imagesy($watermark); imagecopymerge($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity); } if ($repeat === false) { imagecopymerge($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity); } else { $offsetX = $positionX; $offsetY = $positionY; while ($offsetY <= $this->_imageSrcHeight + imagesy($watermark)) { while ($offsetX <= $this->_imageSrcWidth + imagesx($watermark)) { imagecopymerge($this->_imageHandler, $watermark, $offsetX, $offsetY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity); $offsetX += imagesx($watermark); } $offsetX = $positionX; $offsetY += imagesy($watermark); } } imagedestroy($watermark); $this->refreshImageDimensions(); }
/** * Create a GD image resource from file (JPEG, PNG, WBMP and XBM support). * * @param string $filename The image filename. * * @return mixed GD image resource on success, PEAR_Error on failure. * @access public * @static */ function createImageFromFile($filename) { if (!is_file($filename) || !is_readable($filename)) { return PEAR::raiseError('Unable to open file "' . $filename . '"'); } // determine image format list(, , $imgType) = getimagesize($filename); switch ($imgType) { case IMAGETYPE_JPEG: return imagecreatefromjpeg($filename); break; case IMAGETYPE_GIF: return imagecreatefromgif($filename); break; case IMAGETYPE_PNG: return imagecreatefrompng($filename); break; case IMAGETYPE_WBMP: return imagecreatefromwbmp($filename); break; case IMAGETYPE_XBM: return imagecreatefromxbm($filename); break; default: return PEAR::raiseError('Unsupport image type'); } }
static function checkImage($file) { if (!is_string($file) || false === (list($width, $height, $t) = @getimagesize($file))) { return false; } $img = $t == IMAGETYPE_GIF ? @imagecreatefromgif($file) : ($t == IMAGETYPE_WBMP ? @imagecreatefromwbmp($file) : ($t == IMAGETYPE_JPEG ? @imagecreatefromjpeg($file) : ($t == IMAGETYPE_PNG ? @imagecreatefrompng($file) : ($t == IMAGETYPE_XBM ? @imagecreatefromxbm($file) : false)))); return $img !== false; }
/** * Convert uploaded file to PNG * * @deprecated will delete in the next version * @param string $field */ protected function _forcedConvertPng($field) { $file =& $_FILES[$field]; $dotPosition = strrpos($file['name'], '.'); if ($dotPosition !== false) { $file['name'] = substr($file['name'], 0, $dotPosition); } $file['name'] .= '.png'; // We can't use exif extension, because magento doesn't require it. // $fileType = exif_imagetype($file['tmp_name']); list($unnecessaryVar, $unnecessaryVar, $fileType) = getimagesize($file['tmp_name']); unset($unnecessaryVar); if ($fileType != IMAGETYPE_PNG) { switch ($fileType) { case IMAGETYPE_GIF: $img = imagecreatefromgif($file['tmp_name']); imagealphablending($img, false); imagesavealpha($img, true); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($file['tmp_name']); break; case IMAGETYPE_WBMP: $img = imagecreatefromwbmp($file['tmp_name']); break; case IMAGETYPE_XBM: $img = imagecreatefromxbm($file['tmp_name']); break; default: return; } imagepng($img, $file['tmp_name']); imagedestroy($img); } }
/** * @brief Loads an image from a local file. * @param $imageref The path to a local file. * @returns An image resource or false on error */ public function loadFromFile($imagePath = false) { // exif_imagetype throws "read error!" if file is less than 12 byte if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { // Debug output disabled because this method is tried before loadFromBase64? OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: ' . $imagePath, OC_Log::DEBUG); return false; } $iType = exif_imagetype($imagePath); switch ($iType) { case IMAGETYPE_GIF: if (imagetypes() & IMG_GIF) { $this->resource = imagecreatefromgif($imagePath); } else { OC_Log::write('core', 'OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_JPEG: if (imagetypes() & IMG_JPG) { $this->resource = imagecreatefromjpeg($imagePath); } else { OC_Log::write('core', 'OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_PNG: if (imagetypes() & IMG_PNG) { $this->resource = imagecreatefrompng($imagePath); } else { OC_Log::write('core', 'OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_XBM: if (imagetypes() & IMG_XPM) { $this->resource = imagecreatefromxbm($imagePath); } else { OC_Log::write('core', 'OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_WBMP: if (imagetypes() & IMG_WBMP) { $this->resource = imagecreatefromwbmp($imagePath); } else { OC_Log::write('core', 'OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_BMP: $this->resource = $this->imagecreatefrombmp($imagePath); break; /* case IMAGETYPE_TIFF_II: // (intel byte order) break; case IMAGETYPE_TIFF_MM: // (motorola byte order) break; case IMAGETYPE_JPC: break; case IMAGETYPE_JP2: break; case IMAGETYPE_JPX: break; case IMAGETYPE_JB2: break; case IMAGETYPE_SWC: break; case IMAGETYPE_IFF: break; case IMAGETYPE_ICO: break; case IMAGETYPE_SWF: break; case IMAGETYPE_PSD: break; */ /* case IMAGETYPE_TIFF_II: // (intel byte order) break; case IMAGETYPE_TIFF_MM: // (motorola byte order) break; case IMAGETYPE_JPC: break; case IMAGETYPE_JP2: break; case IMAGETYPE_JPX: break; case IMAGETYPE_JB2: break; case IMAGETYPE_SWC: break; case IMAGETYPE_IFF: break; case IMAGETYPE_ICO: break; case IMAGETYPE_SWF: break; case IMAGETYPE_PSD: break; */ default: // this is mostly file created from encrypted file $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); $iType = IMAGETYPE_PNG; OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG); break; } if ($this->valid()) { $this->imageType = $iType; $this->mimeType = image_type_to_mime_type($iType); $this->filePath = $imagePath; } return $this->resource; }
/** * Loads an image from a local file. * * @param bool|string $imagePath The path to a local file. * @return bool|resource An image resource or false on error */ public function loadFromFile($imagePath = false) { // exif_imagetype throws "read error!" if file is less than 12 byte if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { return false; } $iType = exif_imagetype($imagePath); switch ($iType) { case IMAGETYPE_GIF: if (imagetypes() & IMG_GIF) { $this->resource = imagecreatefromgif($imagePath); // Preserve transparency imagealphablending($this->resource, true); imagesavealpha($this->resource, true); } else { $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); } break; case IMAGETYPE_JPEG: if (imagetypes() & IMG_JPG) { $this->resource = imagecreatefromjpeg($imagePath); } else { $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); } break; case IMAGETYPE_PNG: if (imagetypes() & IMG_PNG) { $this->resource = imagecreatefrompng($imagePath); // Preserve transparency imagealphablending($this->resource, true); imagesavealpha($this->resource, true); } else { $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); } break; case IMAGETYPE_XBM: if (imagetypes() & IMG_XPM) { $this->resource = imagecreatefromxbm($imagePath); } else { $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); } break; case IMAGETYPE_WBMP: if (imagetypes() & IMG_WBMP) { $this->resource = imagecreatefromwbmp($imagePath); } else { $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); } break; case IMAGETYPE_BMP: $this->resource = $this->imagecreatefrombmp($imagePath); break; /* case IMAGETYPE_TIFF_II: // (intel byte order) break; case IMAGETYPE_TIFF_MM: // (motorola byte order) break; case IMAGETYPE_JPC: break; case IMAGETYPE_JP2: break; case IMAGETYPE_JPX: break; case IMAGETYPE_JB2: break; case IMAGETYPE_SWC: break; case IMAGETYPE_IFF: break; case IMAGETYPE_ICO: break; case IMAGETYPE_SWF: break; case IMAGETYPE_PSD: break; */ /* case IMAGETYPE_TIFF_II: // (intel byte order) break; case IMAGETYPE_TIFF_MM: // (motorola byte order) break; case IMAGETYPE_JPC: break; case IMAGETYPE_JP2: break; case IMAGETYPE_JPX: break; case IMAGETYPE_JB2: break; case IMAGETYPE_SWC: break; case IMAGETYPE_IFF: break; case IMAGETYPE_ICO: break; case IMAGETYPE_SWF: break; case IMAGETYPE_PSD: break; */ default: // this is mostly file created from encrypted file $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); $iType = IMAGETYPE_PNG; $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); break; } if ($this->valid()) { $this->imageType = $iType; $this->mimeType = image_type_to_mime_type($iType); $this->filePath = $imagePath; } return $this->resource; }
/** * Rotate image * * @param string $path image file * @param int $degree rotete degrees * @param string $bgcolor square background color in #rrggbb format * @param string $destformat image destination format * @return string|false * @author nao-pon * @author Troex Nevelin **/ protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null) { if (($s = @getimagesize($path)) == false) { return false; } $result = false; switch ($this->imgLib) { case 'imagick': try { $img = new imagick($path); } catch (Exception $e) { return false; } $img->rotateImage(new ImagickPixel($bgcolor), $degree); $result = $img->writeImage($path); return $result ? $path : false; break; case 'gd': if ($s['mime'] == 'image/jpeg') { $img = imagecreatefromjpeg($path); } elseif ($s['mime'] == 'image/png') { $img = imagecreatefrompng($path); } elseif ($s['mime'] == 'image/gif') { $img = imagecreatefromgif($path); } elseif ($s['mime'] == 'image/xbm') { $img = imagecreatefromxbm($path); } $degree = 360 - $degree; list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); $bgcolor = imagecolorallocate($img, $r, $g, $b); $tmp = imageRotate($img, $degree, (int) $bgcolor); if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') { $result = imagejpeg($tmp, $path, 100); } else { if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') { $result = imagegif($tmp, $path, 7); } else { $result = imagepng($tmp, $path, 7); } } imageDestroy($img); imageDestroy($tmp); return $result ? $path : false; break; } return false; }
/** * transform source image file (given parameters) and store the result into an other file * @param string $srcFs the path to the image to transform. [gif|jpeg|jpg|jpe|xpm|xbm|wbmp|png] * @param string $targetPath the path of the directory to store the resulting image * @param string $targetName the filename of the resulting image * @param array $params parameters specifying transformations */ public static function transformImage($srcFs, $targetPath, $targetName, $params) { $path_parts = pathinfo($srcFs); $mimeType = self::$mimes[strtolower($path_parts['extension'])]; $quality = !empty($params['quality']) ? $params['quality'] : 100; // Creating an image switch ($mimeType) { case 'image/gif': $image = imagecreatefromgif($srcFs); break; case 'image/jpeg': $image = imagecreatefromjpeg($srcFs); break; case 'image/png': $image = imagecreatefrompng($srcFs); break; case 'image/vnd.wap.wbmp': $image = imagecreatefromwbmp($srcFs); break; case 'image/image/x-xbitmap': $image = imagecreatefromxbm($srcFs); break; case 'image/x-xpixmap': $image = imagecreatefromxpm($srcFs); break; default: return; } if (!empty($params['maxwidth']) && !empty($params['maxheight'])) { $origWidth = imagesx($image); $origHeight = imagesy($image); $constWidth = $params['maxwidth']; $constHeight = $params['maxheight']; $ratio = imagesx($image) / imagesy($image); if ($origWidth < $constWidth && $origHeight < $constHeight) { $params['width'] = $origWidth; $params['height'] = $origHeight; } else { $ratioHeight = $constWidth / $ratio; $ratioWidth = $constHeight * $ratio; if ($ratioWidth > $constWidth) { $constHeight = $ratioHeight; } else { if ($ratioHeight > $constHeight) { $constWidth = $ratioWidth; } } $params['width'] = $constWidth; $params['height'] = $constHeight; } } if (!empty($params['width']) || !empty($params['height'])) { $ancienimage = $image; $resampleheight = imagesy($ancienimage); $resamplewidth = imagesx($ancienimage); $posx = 0; $posy = 0; if (empty($params['width'])) { $finalheight = $params['height']; $finalwidth = $finalheight * imagesx($ancienimage) / imagesy($ancienimage); } else { if (empty($params['height'])) { $finalwidth = $params['width']; $finalheight = $finalwidth * imagesy($ancienimage) / imagesx($ancienimage); } else { $finalwidth = $params['width']; $finalheight = $params['height']; if (!empty($params['omo']) && $params['omo'] == 'true') { if ($params['width'] >= $params['height']) { $resampleheight = $resamplewidth * $params['height'] / $params['width']; } else { $resamplewidth = $resampleheight * $params['width'] / $params['height']; } } } } if (!empty($params['zoom'])) { $resampleheight /= 100 / $params['zoom']; $resamplewidth /= 100 / $params['zoom']; } $posx = imagesx($ancienimage) / 2 - $resamplewidth / 2; $posy = imagesy($ancienimage) / 2 - $resampleheight / 2; if (!empty($params['alignh'])) { if ($params['alignh'] == 'left') { $posx = 0; } else { if ($params['alignh'] == 'right') { $posx = -($resamplewidth - imagesx($ancienimage)); } else { if ($params['alignh'] != 'center') { $posx = -$params['alignh']; } } } } if (!empty($params['alignv'])) { if ($params['alignv'] == 'top') { $posy = 0; } else { if ($params['alignv'] == 'bottom') { $posy = -($resampleheight - imagesy($ancienimage)); } else { if ($params['alignv'] != 'center') { $posy = -$params['alignv']; } } } } $image = imagecreatetruecolor($finalwidth, $finalheight); imagesavealpha($image, true); $tp = imagecolorallocatealpha($image, 0, 0, 0, 127); imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); imagefill($image, 0, 0, $tp); // Because of a strange behavior (ticket #1486), we must fill the background AFTER imagecopyresampled } // The shadow cast adds to the dimension of the image chooses if (!empty($params['shadow'])) { $image = self::createShadow($image, $params); } // Background if (!empty($params['background'])) { $params['background'] = str_replace('#', '', $params['background']); $rgb = array(0, 0, 0); for ($x = 0; $x < 3; $x++) { $rgb[$x] = hexdec(substr($params['background'], 2 * $x, 2)); } $fond = imagecreatetruecolor(imagesx($image), imagesy($image)); imagefill($fond, 0, 0, imagecolorallocate($fond, $rgb[0], $rgb[1], $rgb[2])); imagecopy($fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); $image = $fond; } jFile::createDir($targetPath); // Register switch ($mimeType) { case 'image/gif': imagegif($image, $targetPath . $targetName); break; case 'image/jpeg': imagejpeg($image, $targetPath . $targetName, $quality); break; default: imagepng($image, $targetPath . $targetName); } chmod($targetPath . $targetName, jApp::config()->chmodFile); // Destruction @imagedestroy($image); }
private function setBackground() { $dat = @getimagesize($this->bgimg); if ($dat == FALSE) { return; } switch ($dat[2]) { case 1: $newim = @imagecreatefromgif($this->bgimg); break; case 2: $newim = @imagecreatefromjpeg($this->bgimg); break; case 3: $newim = @imagecreatefrompng($this->bgimg); break; case 15: $newim = @imagecreatefromwbmp($this->bgimg); break; case 16: $newim = @imagecreatefromxbm($this->bgimg); break; default: return; } if (!$newim) { return; } imagecopy($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height); }
/** * Convert uploaded file to PNG * * @param string $originalFile * @param string $destinationFile * @param int|null $originalImageType * @return string */ protected function _forcedConvertPng($originalFile, $destinationFile, $originalImageType = null) { switch ($originalImageType) { case IMAGETYPE_GIF: $img = imagecreatefromgif($originalFile); imagealphablending($img, false); imagesavealpha($img, true); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($originalFile); break; case IMAGETYPE_WBMP: $img = imagecreatefromwbmp($originalFile); break; case IMAGETYPE_XBM: $img = imagecreatefromxbm($originalFile); break; default: return ''; } imagepng($img, $destinationFile); imagedestroy($img); return $destinationFile; }
/** * public method to get the url image and start the whole deal * * @param string $url * @return void * @access public */ public function getUrl($url) { $this->url = $url; // We only want to get the headers, so do a HEAD request instead of GET (default) $opts = array('http' => array('method' => 'HEAD')); $context = stream_context_get_default($opts); $this->headers = get_headers($this->url, 1); if (strstr($this->headers[0], '200') !== FALSE) { if ($this->headers['Content-Length'] < self::max_filesize) { if ($this->is_image($this->headers['Content-Type']) !== FALSE) { // Pretty self-explanatory - figure out which sort of image we're going to be processing and let GD know switch ($this->headers['Content-Type']) { case image_type_to_mime_type(IMAGETYPE_GIF): $this->image = imagecreatefromgif($this->url); break; case image_type_to_mime_type(IMAGETYPE_JPEG): $this->image = imagecreatefromjpeg($this->url); break; case image_type_to_mime_type(IMAGETYPE_PNG): $this->image = imagecreatefrompng($this->url); break; /*case image_type_to_mime_type(IMAGETYPE_BMP): $this->image = $this->imagecreatefrombmp($this->url); break;*/ /*case image_type_to_mime_type(IMAGETYPE_BMP): $this->image = $this->imagecreatefrombmp($this->url); break;*/ case image_type_to_mime_type(IMAGETYPE_WBMP): $this->image = imagecreatefromwbmp($this->url); break; case image_type_to_mime_type(IMAGETYPE_XBM): $this->image = imagecreatefromxbm($this->url); break; default: throw new customException($this->objLanguage->languageText('mod_utilties_unknown_format', 'utilities')); //die('Something\'s gone horribly wrong...'); // If this happens scream very loudly and bang your head into a wall break; } } else { throw new customException($this->objLanguage->languageText('mod_utilties_unknown_format_cannotbedetermined', 'utilities')); } } else { throw new customException($this->objLanguage->languageText('mod_utilties_filetoobig', 'utilities') . round(self::max_filesize / 1024) . 'KB.'); } } else { throw new customException($this->objLanguage->languageText('mod_utilties_urlnoaccess', 'utilities') . $this->url); } }
/** * 跟據文件創建圖像GD 資源 * @param string $fileName 文件名 * @return gd resource */ public function createImageFromFile($fileName = NULL) { if (!$fileName) { $fileName = $this->fileName; $imgType = $this->imageType; } if (!is_readable($fileName) || !file_exists($fileName)) { throw new Exception('Unable to open file "' . $fileName . '"'); } if (!$imgType) { $imageInfo = $this->getImageInfo($fileName); $imgType = $imageInfo[2]; } switch ($imgType) { case IMAGETYPE_GIF: $tempResource = imagecreatefromgif($fileName); break; case IMAGETYPE_JPEG: $tempResource = imagecreatefromjpeg($fileName); break; case IMAGETYPE_PNG: $tempResource = imagecreatefrompng($fileName); break; case IMAGETYPE_WBMP: $tempResource = imagecreatefromwbmp($fileName); break; case IMAGETYPE_XBM: $tempResource = imagecreatefromxbm($fileName); break; default: throw new Exception('Unsupport image type'); } return $tempResource; }
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; }
/** * Set the background of the CAPTCHA image * * @access private * */ function setBackground() { imagefilledrectangle($this->im, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor); imagefilledrectangle($this->tmpimg, 0, 0, $this->image_width * $this->iscale, $this->image_height * $this->iscale, $this->gdbgcolor); if ($this->bgimg == '') { if ($this->background_directory != null && is_dir($this->background_directory) && is_readable($this->background_directory)) { $img = $this->getBackgroundFromDirectory(); if ($img != false) { $this->bgimg = $img; } } } $dat = @getimagesize($this->bgimg); if ($dat == false) { return; } switch ($dat[2]) { case 1: $newim = @imagecreatefromgif($this->bgimg); break; case 2: $newim = @imagecreatefromjpeg($this->bgimg); break; case 3: $newim = @imagecreatefrompng($this->bgimg); break; case 15: $newim = @imagecreatefromwbmp($this->bgimg); break; case 16: $newim = @imagecreatefromxbm($this->bgimg); break; default: return; } if (!$newim) { return; } imagecopyresized($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height, imagesx($newim), imagesy($newim)); }
/** * Create an gd image according to the specified mime type * * @param string $path image file * @param string $mime * @return gd image resource identifier */ protected function gdImageCreate($path, $mime) { switch ($mime) { case 'image/jpeg': return imagecreatefromjpeg($path); case 'image/png': return imagecreatefrompng($path); case 'image/gif': return imagecreatefromgif($path); case 'image/xbm': return imagecreatefromxbm($path); } return false; }
protected function createTmb($vd6fe1d0be6347b8ef2427fa629c04485) { $vd02a169f33469c888396a24dd9d9f1c6 = $this->mimetype($vd6fe1d0be6347b8ef2427fa629c04485); if (!$this->canCreateTmb($vd6fe1d0be6347b8ef2427fa629c04485, $vd02a169f33469c888396a24dd9d9f1c6)) { return false; } $vb068931cc450442b63f5b3d276ea4297 = $this->tmbName($vd6fe1d0be6347b8ef2427fa629c04485); $va8b687c2e93191d90a6d7b8982ceec4a = $this->tmbPath . DIRECTORY_SEPARATOR . $vb068931cc450442b63f5b3d276ea4297; if (($v25d902c24283ab8cfbac54dfa101ad31 = $this->_fopen($vd6fe1d0be6347b8ef2427fa629c04485, 'rb')) == false || ($v90710761341352a9e144d8ad4c92598b = @fopen($va8b687c2e93191d90a6d7b8982ceec4a, 'wb')) == false) { return false; } while (!feof($v25d902c24283ab8cfbac54dfa101ad31)) { fwrite($v90710761341352a9e144d8ad4c92598b, fread($v25d902c24283ab8cfbac54dfa101ad31, 8192)); } $this->_fclose($v25d902c24283ab8cfbac54dfa101ad31, $vd6fe1d0be6347b8ef2427fa629c04485); fclose($v90710761341352a9e144d8ad4c92598b); if (($v03c7c0ace395d80182db07ae2c30f034 = @getimagesize($va8b687c2e93191d90a6d7b8982ceec4a)) == false) { return false; } $result = false; $v88029fd68f54ae31758f44d7b965868f = $this->tmbSize; list($v9dd4e461268c8034f5c8564e155c67a6, $v415290769594460e2e485922904f345d, $v84b16781293d33e80d74ef9d63280bc0, $v55fc30b57cc1b80e8f68104931fbf716) = $this->tmbEffects($v03c7c0ace395d80182db07ae2c30f034[0], $v03c7c0ace395d80182db07ae2c30f034[1], $v88029fd68f54ae31758f44d7b965868f, $this->options['tmbCrop']); switch ($this->imgLib) { case 'imagick': try { $vb798abe6e1b1318ee36b0dcb3fb9e4d3 = new imagick($va8b687c2e93191d90a6d7b8982ceec4a); } catch (Exception $ve1671797c52e15f763380b45e841ec32) { return false; } $vb798abe6e1b1318ee36b0dcb3fb9e4d3->contrastImage(1); if ($this->options['tmbCrop'] == false) { $va8a63b4d63a08aed720d0f5f249e07d9 = new Imagick(); $va8a63b4d63a08aed720d0f5f249e07d9->newImage($v88029fd68f54ae31758f44d7b965868f, $v88029fd68f54ae31758f44d7b965868f, new ImagickPixel($this->options['tmbBgColor'])); $va8a63b4d63a08aed720d0f5f249e07d9->setImageFormat('png'); $vb798abe6e1b1318ee36b0dcb3fb9e4d3->resizeImage($v84b16781293d33e80d74ef9d63280bc0, $v55fc30b57cc1b80e8f68104931fbf716, NULL, true); $va8a63b4d63a08aed720d0f5f249e07d9->compositeImage($vb798abe6e1b1318ee36b0dcb3fb9e4d3, imagick::COMPOSITE_OVER, $v9dd4e461268c8034f5c8564e155c67a6, $v415290769594460e2e485922904f345d); $result = $va8a63b4d63a08aed720d0f5f249e07d9->writeImage($va8b687c2e93191d90a6d7b8982ceec4a); } else { $result = $vb798abe6e1b1318ee36b0dcb3fb9e4d3->cropThumbnailImage($v88029fd68f54ae31758f44d7b965868f, $v88029fd68f54ae31758f44d7b965868f) && $vb798abe6e1b1318ee36b0dcb3fb9e4d3->writeImage($va8b687c2e93191d90a6d7b8982ceec4a); } break; case 'gd': if ($v03c7c0ace395d80182db07ae2c30f034['mime'] == 'image/jpeg') { $vb798abe6e1b1318ee36b0dcb3fb9e4d3 = imagecreatefromjpeg($va8b687c2e93191d90a6d7b8982ceec4a); } elseif ($v03c7c0ace395d80182db07ae2c30f034['mime'] == 'image/png') { $vb798abe6e1b1318ee36b0dcb3fb9e4d3 = imagecreatefrompng($va8b687c2e93191d90a6d7b8982ceec4a); } elseif ($v03c7c0ace395d80182db07ae2c30f034['mime'] == 'image/gif') { $vb798abe6e1b1318ee36b0dcb3fb9e4d3 = imagecreatefromgif($va8b687c2e93191d90a6d7b8982ceec4a); } elseif ($v03c7c0ace395d80182db07ae2c30f034['mime'] == 'image/xbm') { $vb798abe6e1b1318ee36b0dcb3fb9e4d3 = imagecreatefromxbm($va8b687c2e93191d90a6d7b8982ceec4a); } if (isset($vb798abe6e1b1318ee36b0dcb3fb9e4d3) && $vb798abe6e1b1318ee36b0dcb3fb9e4d3 && false != ($vfa816edb83e95bf0c8da580bdfd491ef = imagecreatetruecolor($v88029fd68f54ae31758f44d7b965868f, $v88029fd68f54ae31758f44d7b965868f))) { if ($this->options['tmbCrop'] == false) { if ($this->options['tmbBgColor'] == 'transparent') { list($v4b43b0aee35624cd95b910189b3dc231, $vb2f5ff47436671b6e533d8dc3614845d, $v92eb5ffee6ae2fec3ad71c777531578f) = array(0, 0, 255); } else { list($v4b43b0aee35624cd95b910189b3dc231, $vb2f5ff47436671b6e533d8dc3614845d, $v92eb5ffee6ae2fec3ad71c777531578f) = sscanf($this->options['tmbBgColor'], "#%02x%02x%02x"); } $ve12224dadfacf7a79886df59f202a403 = imagecolorallocate($vfa816edb83e95bf0c8da580bdfd491ef, $v4b43b0aee35624cd95b910189b3dc231, $vb2f5ff47436671b6e533d8dc3614845d, $v92eb5ffee6ae2fec3ad71c777531578f); if ($this->options['tmbBgColor'] == 'transparent') { $ve12224dadfacf7a79886df59f202a403 = imagecolortransparent($vfa816edb83e95bf0c8da580bdfd491ef, $ve12224dadfacf7a79886df59f202a403); } imagefill($vfa816edb83e95bf0c8da580bdfd491ef, 0, 0, $ve12224dadfacf7a79886df59f202a403); if (!imagecopyresampled($vfa816edb83e95bf0c8da580bdfd491ef, $vb798abe6e1b1318ee36b0dcb3fb9e4d3, $v9dd4e461268c8034f5c8564e155c67a6, $v415290769594460e2e485922904f345d, 0, 0, $v84b16781293d33e80d74ef9d63280bc0, $v55fc30b57cc1b80e8f68104931fbf716, $v03c7c0ace395d80182db07ae2c30f034[0], $v03c7c0ace395d80182db07ae2c30f034[1])) { return false; } } else { if (!imagecopyresampled($vfa816edb83e95bf0c8da580bdfd491ef, $vb798abe6e1b1318ee36b0dcb3fb9e4d3, 0, 0, $v9dd4e461268c8034f5c8564e155c67a6, $v415290769594460e2e485922904f345d, $v88029fd68f54ae31758f44d7b965868f, $v88029fd68f54ae31758f44d7b965868f, $v84b16781293d33e80d74ef9d63280bc0, $v55fc30b57cc1b80e8f68104931fbf716)) { return false; } } $result = imagepng($vfa816edb83e95bf0c8da580bdfd491ef, $va8b687c2e93191d90a6d7b8982ceec4a, 7); imagedestroy($vb798abe6e1b1318ee36b0dcb3fb9e4d3); imagedestroy($vfa816edb83e95bf0c8da580bdfd491ef); } break; } return $result ? $vb068931cc450442b63f5b3d276ea4297 : false; }
/** * 画出验证码图片 * * @param int $width 验证码图片宽度 * @param int $height 验证码图片高度 * @param string $bg 附加背景图地址 * @return string 返回验证码 */ function draw($width = 120, $height = 60, $bg = '') { /** * 设置颜色 */ $image = imagecreatetruecolor($width, $height) or die('Cannot initialize new GD image stream'); $nscolor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰颜色 if (empty($this->bgcolor)) { $bgcolor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //背景颜色 } else { $rgb = $this->html2rgb($this->bgcolor); $bgcolor = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]); } imagefilledrectangle($image, 0, 0, $width, $height, $bgcolor); //设置背景颜色 /** * 背景处理 */ $this->background = $bg ? $bg : $this->background; if ($this->background && file_exists($this->background)) { $dat = getimagesize($this->background); switch ($dat[2]) { case 1: $newim = imagecreatefromgif($this->background); break; case 2: $newim = imagecreatefromjpeg($this->background); break; case 3: $newim = imagecreatefrompng($this->background); break; case 15: $newim = imagecreatefromwbmp($this->background); break; case 16: $newim = imagecreatefromxbm($this->background); break; default: $newim = null; } /** * 将背景合成到验证码图片中 */ if ($newim) { if ($this->copy_bg_width <= 0) { $this->copy_bg_width = $width; } if ($this->copy_bg_height <= 0) { $this->copy_bg_height = $height; } imagecopyresampled($image, $newim, 0, 0, 0, 0, $this->copy_bg_width, $this->copy_bg_height, imagesx($newim), imagesy($newim)); } } else { for ($i = 0; $i < $this->disturb_size; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $nscolor); } for ($i = 0; $i < $this->disturb_size; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $nscolor); } } /** * 创建文本框与文本 */ if (empty($this->words)) { $this->generate_words(); } $font_size = $this->font_size ? $this->font_size : ceil(($width + $height) / 2 * 0.36); $textbox = imagettfbbox($font_size, 0, $this->font, $this->words) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; if ($this->text_margin_top > 0) { $y = $this->text_margin_top; //固定字母对象顶边的外延边距 } else { $y = ($height - $textbox[5]) / 2; //不固定字母对象顶边的外延边距 } /** * 生成字母并定位置 */ $strlen = strlen($this->words); for ($i = 0; $i < $strlen; ++$i) { $text_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $this->words[$i]); $x = $x + $font_size / 1.3; if ($this->text_margin_top <= 0) { $y = rand($y - $height / 5, $y + $height / 5); } } /** * 输出图片 */ header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); switch ($this->image_type) { case 'jpg': case 'jpeg': header('Content-Type: image/jpeg'); imagejpeg($image); break; case 'gif': header('Content-type: image/gif'); imagegif($image); break; default: header('Content-Type: image/png'); imagepng($image); break; } imagedestroy($image); return $this->words; }
private function _load_image($path) { list($w, $h, $type) = getimagesize($path); switch ($type) { case IMAGETYPE_GIF: $this->_format = 'gif'; return imagecreatefromgif($path); case IMAGETYPE_JPEG: $this->_format = 'jpg'; return imagecreatefromjpeg($path); case IMAGETYPE_PNG: $this->_format = 'png'; return imagecreatefrompng($path); case IMAGETYPE_SWF: $this->_format = 'swf'; return imagecreatefromswf($path); case IMAGETYPE_WBMP: $this->_format = 'wbmp'; return imagecreatefromwbmp($path); case IMAGETYPE_XBM: $this->_format = 'xbm'; return imagecreatefromxbm($path); default: return imagecreatefromstring(file_get_contents($path)); } return false; }
function loadImageFromFile($imageInfo) { $_result = 0; $filelocation = $imageInfo['file_location']; //$filelocation = dirname($this->_filename).'/'.$filelocation; switch ($imageInfo['type']) { case 1: $_result = @imagecreatefromgif($filelocation); break; case 2: $_result = @imagecreatefromjpeg($filelocation); break; case 3: $_result = @imagecreatefrompng($filelocation); break; case 4: $_result = @imagecreatefromswf($filelocation); break; case 6: $_result = @imagecreatefromwbmp($filelocation); break; case 15: $_result = @imagecreatefromxbm($filelocation); break; } $_colorCount = ImageColorsTotal($_result); //$this->log->info[] = 'testing image: '. basename($filelocation) . ' colors: '.$_colorCount . //" size:[{$imageInfo['width']}x{$imageInfo['height']}]\n"; if ($this->_maxColors < $_colorCount) { $this->_maxColors = $_colorCount; } if ($_colorCount == 0) { $this->_trueColor = true; } if (!$_result) { die("ERROR: Can not open file: {$filelocation}\n"); } return $_result; }