Пример #1
0
 /**
  * Rotate image by the given angle
  * Uses a fast rotation algorythm for custom angles
  * or lines copy for multiple of 90 degrees
  *
  * @param int       $angle      Rotation angle
  * @param array     $options    array(  'autoresize'=>true|false,
  *                                      'color_mask'=>array(r,g,b), named color or #rrggbb
  *                                   )
  * @author Pierre-Alain Joye
  * @return mixed none or a PEAR error object on error
  * @see PEAR::isError()
  */
 function rotate($angle, $options = null)
 {
     if (function_exists('imagerotate')) {
         $white = imagecolorallocatealpha($this->imageHandle, 255, 255, 255, 127);
         $this->imageHandle = imagerotate($this->imageHandle, $angle, $white);
         return true;
     }
     if ($options == null) {
         $autoresize = true;
         $color_mask = array(105, 255, 255);
     } else {
         extract($options);
     }
     while ($angle <= -45) {
         $angle += 360;
     }
     while ($angle > 270) {
         $angle -= 360;
     }
     $t = deg2rad($angle);
     if (!is_array($color_mask)) {
         if ($color[0] == '#') {
             $this->colorhex2colorarray($color_mask);
         } else {
             include_once 'Image/Transform/Driver/ColorDefs.php';
             $color = isset($colornames[$color_mask]) ? $colornames[$color_mask] : false;
         }
     }
     // Do not round it, too much lost of quality
     $cosT = cos($t);
     $sinT = sin($t);
     $img =& $this->imageHandle;
     $width = $max_x = $this->img_x;
     $height = $max_y = $this->img_y;
     $min_y = 0;
     $min_x = 0;
     $x1 = round($max_x / 2, 0);
     $y1 = round($max_y / 2, 0);
     if ($autoresize) {
         $t = abs($t);
         $a = round($angle, 0);
         switch ((int) $angle) {
             case 0:
                 $width2 = $width;
                 $height2 = $height;
                 break;
             case 90:
                 $width2 = $height;
                 $height2 = $width;
                 break;
             case 180:
                 $width2 = $width;
                 $height2 = $height;
                 break;
             case 270:
                 $width2 = $height;
                 $height2 = $width;
                 break;
             default:
                 $width2 = (int) abs(sin($t) * $height + cos($t) * $width);
                 $height2 = (int) abs(cos($t) * $height + sin($t) * $width);
         }
         $width2 -= $width2 % 2;
         $height2 -= $height2 % 2;
         $d_width = abs($width - $width2);
         $d_height = abs($height - $height2);
         $x_offset = $d_width / 2;
         $y_offset = $d_height / 2;
         $min_x2 = -abs($x_offset);
         $min_y2 = -abs($y_offset);
         $max_x2 = $width2;
         $max_y2 = $height2;
     }
     $img2 = @$this->newImgPreserveAlpha(imagecreateTrueColor($width2, $height2));
     if (!is_resource($img2)) {
         return false;
         /*PEAR::raiseError('Cannot create buffer for the rotataion.',
           null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
     }
     $this->img_x = $width2;
     $this->img_y = $height2;
     imagepalettecopy($img2, $img);
     $mask = imagecolorallocatealpha($img2, $color_mask[0], $color_mask[1], $color_mask[2], 127);
     // use simple lines copy for axes angles
     switch ((int) $angle) {
         case 0:
             imagefill($img2, 0, 0, $mask);
             for ($y = 0; $y < $max_y; $y++) {
                 for ($x = $min_x; $x < $max_x; $x++) {
                     $c = @imagecolorat($img, $x, $y);
                     imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c);
                 }
             }
             break;
         case 90:
             imagefill($img2, 0, 0, $mask);
             for ($x = $min_x; $x < $max_x; $x++) {
                 for ($y = $min_y; $y < $max_y; $y++) {
                     $c = imagecolorat($img, $x, $y);
                     imagesetpixel($img2, $max_y - $y - 1, $x, $c);
                 }
             }
             break;
         case 180:
             imagefill($img2, 0, 0, $mask);
             for ($y = 0; $y < $max_y; $y++) {
                 for ($x = $min_x; $x < $max_x; $x++) {
                     $c = @imagecolorat($img, $x, $y);
                     imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c);
                 }
             }
             break;
         case 270:
             imagefill($img2, 0, 0, $mask);
             for ($y = 0; $y < $max_y; $y++) {
                 for ($x = $max_x; $x >= $min_x; $x--) {
                     $c = @imagecolorat($img, $x, $y);
                     imagesetpixel($img2, $y, $max_x - $x - 1, $c);
                 }
             }
             break;
             // simple reverse rotation algo
         // simple reverse rotation algo
         default:
             $i = 0;
             for ($y = $min_y2; $y < $max_y2; $y++) {
                 // Algebra :)
                 $x2 = round(($min_x2 - $x1) * $cosT + (($y - $y1) * $sinT + $x1), 0);
                 $y2 = round(($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1, 0);
                 for ($x = $min_x2; $x < $max_x2; $x++) {
                     // Check if we are out of original bounces, if we are
                     // use the default color mask
                     if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) {
                         $c = imagecolorat($img, $x2, $y2);
                     } else {
                         $c = $mask;
                     }
                     imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c);
                     // round verboten!
                     $x2 += $cosT;
                     $y2 -= $sinT;
                 }
             }
             break;
     }
     $this->old_image = $this->imageHandle;
     $this->imageHandle = $img2;
     return true;
 }
Пример #2
0
public function updateImage1($picname,$path,$prix="s_",$maxwidth=104,$maxheight=104){
	//1. 定义获取基本信息
	$path = rtrim($path,"/"); //去除后面多余的"/"
	$info1 = getimagesize($path."/".$picname);  //获取图片文件的属性信息
	$width = $info1[0]; //原图片的宽度
	$height = $info1[1]; //原图片的高度
	
	//2. 计算压缩后的尺寸
	if(($maxwidth/$width)<($maxheight/$height)){
		$w=$maxwidth;//新图片的宽度
		$h=($maxwidth/$width)*$height;//新图片的高度
	}else{
		$h=$maxheight;//新图片的宽度
		$w=($maxheight/$height)*$width;//新图片的高度
	}


	//3. 创建图像源
	$newim =imagecreateTrueColor($w,$h); //新图片源
	//根据原图片类型来创建图片源
	switch($info1[2]){
		case 1: //gif格式
			$srcim = imageCreateFromgif($path."/".$picname);
			break;
		case 2: //jpeg格式
			$srcim = imageCreateFromjpeg($path."/".$picname);
			break;
		case 3: //png格式
			$srcim = imageCreateFrompng($path."/".$picname);
			break;
		case 6: //bmp格式
			$srcim = imageCreateFrompng($path."/".$picname);
			break;
		default:
			exit("无效的图片格式!");
			break;
	}

	//4. 执行缩放处理
	imagecopyresampled($newim,$srcim,0,0,0,0,$w,$h,$width,$height);


	//5. 输出保存绘画
	//header("Content-Type:".$info['mime']); //设置响应类型为图片格式
	//根据原图片类型来保存新图片
	switch($info1[2]){
		case 1: //gif格式
			imagegif($newim,$path."/".$prix.$picname); //保存
			//imagegif($newim);//输出
			break;
		case 2: //jpeg格式
			imagejpeg($newim,$path."/".$prix.$picname);
			//imagejpeg($newim);
			break;
		case 3: //png格式
			imagepng($newim,$path."/".$prix.$picname);
			//imagepng($newim);
			break;
		case 6: //bmp格式
			imagebmp($newim,$path."/".$prix.$picname);
			break;
	}

	//6. 销毁资源
	imageDestroy($newim);
	imageDestroy($srcim);
}
Пример #3
0
 /**
  * The filename must have an extension and it must be one of the following: jpg, gif, png, jpeg, or bmp.
  * If a thumbnail already exists in the source files' directory, this function automatically appends an
  * incremental numeric suffix.
  *
  * If the output filename already exists on disk, this function will revert to auto-creating a thumbnail filename.
  *
  * @param string  $filename        The filename to process
  * @param string  $outputFilename  The name of the file to write to
  * @param string  $outputDirectory The name of the directory that holds our outputfile
  * @param integer $width           The width in pixels of the created thumbnail
  * @param integer $height          The height in pixels of the created thumbnail
  * @param boolean $max             If set to true then the image aspect ratio is preserved
  *                                   with the max length of either width or height being {@link $width} or {@link $height}
  * @param boolean $crop            If set to true, the image will be cropped to the dimensions specified.
  *                                 If set to false, the image will be resized.
  *
  * @return string the name of the file where the thumbnail can be found
  * @throws ThumbnailsException If any of the parameters are invalid.
  *
  * @see generateThumbnail(...)
  */
 protected function generateThumbnail($filename, $outputFilename, $outputDirectory, $width = 0, $height = 0, $max = false, $crop = false, $ext = null, $quality = null)
 {
     if (!is_file($filename)) {
         throw new ThumbnailsException("Source file does not exist '" . $filename . "'");
     }
     $path = pathinfo($filename);
     if (empty($path['extension'])) {
         throw new ThumbnailsException("Source file does not have an extension '" . $filename . "'");
     }
     $originalExt = strtolower(trim($path['extension']));
     if (empty($ext)) {
         $ext = $originalExt;
     }
     if (!in_array($ext, $this->supportedExtensions)) {
         throw new ThumbnailsException("Thumbnail file extension [{$ext}] is not supported");
     }
     if ($outputDirectory != null && !is_dir($outputDirectory)) {
         throw new ThumbnailsException("Output directory does not exist or is not a valid directory '{$outputDirectory}'");
     }
     //parameter validation
     if ($width === 0 && $height === 0) {
         throw new ThumbnailsException("Width and/or height must be specified");
     }
     if (!is_int($width)) {
         throw new ThumbnailsException("Width [{$width}] is not a valid integer");
     }
     if (!is_int($height)) {
         throw new ThumbnailsException("Height [{$height}] is not a valid integer");
     }
     if ($width < 0) {
         throw new ThumbnailsException("Width cannot be negative");
     }
     if ($height < 0) {
         throw new ThumbnailsException("Height cannot be negative");
     }
     if ($max === true && ($width === 0 || $height === 0)) {
         throw new ThumbnailsException("If max is true then width and height must be positive");
     }
     if ($crop === true && ($width === 0 || $height === 0 || $max === true)) {
         throw new ThumbnailsException("If crop is true then width and height must be positive and max must be false");
     }
     if ($outputDirectory == null) {
         $outputDirectory = $path['dirname'];
     } else {
         $outputDirectory = rtrim($outputDirectory, '/');
     }
     //check for existing thumbnail in same directory, increment filename
     $outfile = $outputFilename == null ? "{$path['filename']}{$this->autoNameSuffix}.{$ext}" : $outputFilename;
     $inc = 1;
     while (is_file("{$outputDirectory}/{$outfile}")) {
         $outfile = "{$path['filename']}{$this->autoNameSuffix}-{$inc}.{$ext}";
         $inc++;
     }
     //list($origwidth, $origheight) = getimagesize($filename);
     //build ImageMagick operation
     if ($this->convertMode == 'exec_imagick') {
         if ($max === true) {
             $op = "-resize {$width}x{$height}\\>";
         } else {
             if ($crop === true) {
                 if ($this->imageMagickCompatMode == false) {
                     // As of IM v6.3.8-3 the special resize option flag '^' was added
                     // to make cutting the image to fit easier.
                     $op = "-resize {$width}x{$height}\\>^ -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                 } else {
                     // Complex trickiness to perform the cut to fit resize
                     // Calculate the thumbnail aspect ratio.
                     // > 1 is a wide thumb
                     // < 1 is a tall thumb
                     // 1 is a square thumb
                     $thumb_aspect_ratio = $width / $height;
                     // Get the dimensions of the image
                     $dimensions = getimagesize($filename);
                     $image_aspect_ratio = $dimensions[0] / $dimensions[1];
                     // Definitions:
                     //  width-crop = Resize the image to the full width of the thumbnail and trim the top and bottom
                     //  height-crop = Resize the image to the full height of the thumbnail and trip the sides
                     // Behavior:
                     // If image_aspect_ratio < thumb_aspect_ratio perform a width-crop
                     // If image_aspect_ratio >= thumb_aspect_ratio perform a height-crop
                     if ($image_aspect_ratio < $thumb_aspect_ratio) {
                         $op = "-resize {$width}x\\> -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                     } else {
                         $op = "-resize x{$height}\\> -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                     }
                 }
             } else {
                 if ($height === 0) {
                     $op = "-resize {$width}x\\>";
                 } else {
                     if ($width === 0) {
                         $op = "-resize x{$height}\\>";
                     } else {
                         $op = "-resize {$width}x{$height}!\\>";
                     }
                 }
             }
         }
         $qualityArg = $quality ? '-quality ' . escapeshellarg($quality) : ($qualityArg = '');
         $outPath = escapeshellarg("{$outputDirectory}/{$outfile}");
         //full ImageMagick command; redirect STDERR to STDOUT
         $filename = escapeshellarg($filename);
         $cmd = "{$this->pathToImageMagickConvert} {$filename} {$op} {$qualityArg} {$outPath} 2>&1";
         $retval = 1;
         $output = array();
         $this->Logger->debug("Excecuting [{$cmd}]");
         exec($cmd, $output, $retval);
         if ($retval > 0) {
             throw new ThumbnailsException("Generation failed '" . $cmd . "\n" . implode("\n", $output) . "'");
         }
     } elseif ($this->convertMode == 'pecl_imagick') {
         $image = new Imagick($filename);
         if ($max == true) {
             $image->scaleImage($width, $height, true);
         } elseif ($crop === true) {
             // Because Imagick::cropThumbnailImage() doesn't support different gravities,
             // we need to expand the functionality out here. PITA!
             if ($this->cropGravity == 'center') {
                 $image->cropThumbnailImage($width, $height);
             } else {
                 // Resize full image by default so the smallest edge is
                 // the max width/height
                 if ($image->getImageWidth() > $image->getImageHeight()) {
                     $image->scaleImage(0, $height);
                 } else {
                     $image->scaleImage($width, 0);
                 }
                 // Then crop out the needed section.
                 $image_width = $image->getImageWidth();
                 $image_height = $image->getImageHeight();
                 switch (strtolower($this->cropGravity)) {
                     case 'northwest':
                         $x = $image_width - $width;
                         $y = 0;
                         break;
                     case 'north':
                         $x = $image_width / 2 - $width / 2;
                         $y = 0;
                         break;
                     case 'northeast':
                         $x = 0;
                         $y = 0;
                         break;
                     case 'west':
                         $x = 0;
                         $y = $image_height / 2 - $height / 2;
                         break;
                     case 'east':
                         $x = $image_width - $width;
                         $y = $image_height / 2 - $height / 2;
                         break;
                     case 'southwest':
                         $x = 0;
                         $y = $image_height - $height;
                         break;
                     case 'south':
                         $x = $image_width / 2 - $width / 2;
                         $y = $image_height - $height;
                         break;
                     case 'southeast':
                         $x = $image_width - $width;
                         $y = $image_height - $height;
                         break;
                     default:
                         throw new ThumbnailsException("Unsupported crop gravity: {$this->cropGravity}");
                 }
                 $x = floor($x);
                 $y = floor($y);
                 $image->cropImage($width, $height, $x, $y);
             }
         } elseif ($height === 0) {
             $image->scaleImage($width, $height);
         } elseif ($width === 0) {
             $image->scaleImage($width, $height);
         } else {
             $image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
         }
         if ($quality) {
             $image->setImageCompressionQuality($quality);
         }
         // Imagick will infer the file format from the filename extension
         $image->writeImage("{$outputDirectory}/{$outfile}");
         $image->clear();
         $image->destroy();
     } elseif ($this->convertMode == 'gd') {
         $origImage = null;
         switch ($originalExt) {
             case 'jpg':
             case 'jpeg':
                 $origImage = imagecreatefromJPEG($filename);
                 break;
             case 'gif':
                 $origImage = imagecreatefromGIF($filename);
                 break;
             case 'png':
                 $origImage = imagecreatefromPNG($filename);
                 break;
             case 'bmp':
                 $origImage = imagecreatefromWBMP($filename);
                 break;
             default:
                 throw new ThumbnailsException('GD does not know how to handle .' . $originalExt . ' files.');
         }
         if (function_exists('imageantialias')) {
             imageantialias($origImage, true);
         }
         $image_attr = getimagesize($filename);
         $image_width = $image_attr[0];
         $image_height = $image_attr[1];
         $dst_x = 0;
         $dst_y = 0;
         $src_x = null;
         $src_y = null;
         $dst_w = null;
         $dst_h = null;
         $src_w = null;
         $src_h = null;
         if ($max === true) {
             // resize to dimensions, preserving aspect ratio
             $src_x = 0;
             $src_y = 0;
             $src_w = $image_width;
             $src_h = $image_height;
             if ($image_width > $image_height) {
                 $dst_w = $width;
                 $dst_h = (int) floor($image_height * ($width / $image_width));
             } else {
                 $dst_h = $height;
                 $dst_w = (int) floor($image_width * ($height / $image_height));
             }
         } else {
             if ($crop === true) {
                 // crop the image with cropGravity
                 $dst_w = $width;
                 $dst_h = $height;
                 // By default, resize the whole image
                 $src_w = $image_attr[0];
                 $src_h = $image_attr[1];
                 $thumb_aspect_ratio = $width / $height;
                 $image_aspect_ratio = $image_attr[0] / $image_attr[1];
                 if ($image_aspect_ratio < $thumb_aspect_ratio) {
                     // width-crop
                     $src_w = $image_attr[0];
                     // original-width
                     $resize_ratio = $image_attr[0] / $width;
                     // original-width / thumbnail-width
                     $src_h = floor($height * $resize_ratio);
                     // thumbnail-height * original-width / thumbnail-width
                 } else {
                     // height-crop
                     $src_h = $image_attr[1];
                     // original-height
                     $resize_ratio = $image_attr[1] / $height;
                     // original-height / thumbnail-height
                     $src_w = floor($width * $resize_ratio);
                     // thumbnail-width * original-height / thumbnail-height
                 }
                 $dst_x = 0;
                 $dst_y = 0;
                 $dst_w = $width;
                 $dst_h = $height;
                 switch (strtolower($this->cropGravity)) {
                     case 'center':
                         $src_x = floor(($image_attr[0] - $src_w) / 2);
                         $src_y = floor(($image_attr[1] - $src_h) / 2);
                         break;
                     case 'northeast':
                         $src_x = 0;
                         $src_y = 0;
                         break;
                     case 'north':
                         $src_x = floor(($image_attr[0] - $src_w) / 2);
                         $src_y = 0;
                         break;
                     case 'south':
                         $src_x = floor($image_attr[0] - $image_width);
                         $src_y = floor($image_attr[1] - $image_height);
                         break;
                     default:
                         throw new ThumbnailsException("Unsupported cropGravity for GD: {$this->cropGravity}");
                 }
             } else {
                 if ($height === 0) {
                     // resize to max width, preserving aspect ratio
                     $src_x = 0;
                     $src_y = 0;
                     $src_w = $image_width;
                     $src_h = $image_height;
                     $dst_w = $width;
                     $dst_h = $image_height * ($width / $image_width);
                 } else {
                     if ($width === 0) {
                         // resize to max height, preserving aspect ratio
                         $src_x = 0;
                         $src_y = 0;
                         $src_w = $image_width;
                         $src_h = $image_height;
                         $dst_h = $height;
                         $dst_w = $image_width * ($height / $image_height);
                     } else {
                         // resize, ignoring aspect ratio
                         $src_x = 0;
                         $src_y = 0;
                         $src_w = $image_width;
                         $src_h = $image_height;
                         $dst_w = $width;
                         $dst_h = $height;
                     }
                 }
             }
         }
         $newImage = imagecreateTrueColor($dst_w, $dst_h);
         //preserve transparency
         $transindex = -1;
         if ($ext == 'gif') {
             imagealphablending($newImage, false);
             $transindex = imagecolortransparent($origImage);
             if ($transindex >= 0) {
                 $transcol = imagecolorsforindex($origImage, $transindex);
                 $transindex = imagecolorallocatealpha($newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
                 imagefill($newImage, 0, 0, $transindex);
             }
         }
         @imagecopyResampled($newImage, $origImage, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
         //preserve transparency
         if ($ext == 'gif') {
             if ($transindex >= 0) {
                 imagecolortransparent($newImage, $transindex);
                 for ($y = 0; $y < $dst_h; ++$y) {
                     for ($x = 0; $x < $dst_w; ++$x) {
                         if ((imagecolorat($newImage, $x, $y) >> 24 & 0x7f) >= 100) {
                             imagesetpixel($newImage, $x, $y, $transindex);
                         }
                     }
                 }
                 imagetruecolortopalette($newImage, true, 255);
                 imagesavealpha($newImage, false);
             }
         }
         // echo "<pre>"; var_dump($dst_h); die("</pre>");
         $outfilepath = "{$outputDirectory}/{$outfile}";
         switch ($ext) {
             case 'jpg':
             case 'jpeg':
                 imageJPEG($newImage, $outfilepath, $quality);
                 break;
             case 'gif':
                 imageGIF($newImage, $outfilepath);
                 break;
             case 'png':
                 imagePNG($newImage, $outfilepath, $quality);
                 break;
             case 'bmp':
                 imageWBMP($newImage, $outfilepath);
                 break;
         }
         imagedestroy($newImage);
         imagedestroy($origImage);
     }
     return $outfile;
 }
Пример #4
0
    $img_therbar = "../../pixmaps/therm/thermbar_orange.jpg";
} elseif ($level >= 40) {
    $img_ther = "../../pixmaps/therm/therm_yellow.jpg";
    $img_therbar = "../../pixmaps/therm/thermbar_yellow.jpg";
} elseif ($level >= 20) {
    $img_ther = "../../pixmaps/therm/therm_yellgre.jpg";
    $img_therbar = "../../pixmaps/therm/thermbar_yellgre.jpg";
} else {
    $img_ther = "../../pixmaps/therm/therm_green.jpg";
    $img_therbar = "../../pixmaps/therm/thermbar_green.jpg";
}
$t_unit = 'none';
$t_max = 100;
$t_current = $level;
$finalimagewidth = max(strlen($t_max), strlen($t_current)) * 25;
$finalimage = imagecreateTrueColor(60 + $finalimagewidth, 405);
$white = imagecolorallocate($finalimage, 255, 255, 255);
$black = imagecolorallocate($finalimage, 0, 0, 0);
$red = imagecolorallocate($finalimage, 255, 0, 0);
$orange = imagecolorallocate($finalimage, 238, 120, 46);
$yellow = imagecolorallocate($finalimage, 252, 194, 0);
$yellgr = imagecolorallocate($finalimage, 179, 174, 8);
$green = imagecolorallocate($finalimage, 51, 142, 5);
imagefill($finalimage, 0, 0, $white);
ImageAlphaBlending($finalimage, true);
$thermImage = imagecreatefromjpeg($img_ther);
$tix = ImageSX($thermImage);
$tiy = ImageSY($thermImage);
//ImageCopy($finalimage,$thermImage,0,0,0,0,$tix,$tiy);
ImageCopy($finalimage, $thermImage, 17, 0, 0, 0, $tix, $tiy);
$thermbarImage = ImageCreateFromjpeg($img_therbar);
Пример #5
0
<?php