示例#1
0
 /**
  * {@inheritDoc}
  */
 public function save($item, $key = '')
 {
     $id = $item['id'];
     $outputType = strtolower($this->getParm('type'));
     $path = ($this->publicPath ? $this->publicPath . DIRECTORY_SEPARATOR : '') . $this->getParm('path');
     $compression = $this->getParm('compression');
     $key = ($key ? $key . '.' : '') . $this->name;
     $file = $this->request->file($key);
     if ($file) {
         if ($file->isValid()) {
             $tmpFile = $path . DIRECTORY_SEPARATOR . $id;
             $outputFile = $tmpFile . '.' . $outputType;
             $file->move($path, $id);
             list($width, $height, $inputType) = getimagesize($tmpFile);
             if ($inputType === imageConvertType($outputType)) {
                 rename($tmpFile, $outputFile);
             } else {
                 imageSave($outputType, $outputFile, imageOpen($inputType, $tmpFile), $compression);
                 unlink($tmpFile);
             }
         }
     } elseif (!$this->request->input($key)) {
         $this->delete($item);
     }
 }
示例#2
0
 /**
  * Displays image with given file name, size and compression.
  *
  * @param Request $request
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request, Repository $config)
 {
     $file = $request->get('src');
     $width = (int) $request->get('width');
     $height = (int) $request->get('height');
     $compression = $request->get('compression') ? (int) $request->get('compression') : 80;
     if (!$file) {
         abort(404);
     }
     if (file_exists($file)) {
         if (!$width && !$height) {
             $cacheFile = $file;
         } else {
             $ext = pathinfo($file, PATHINFO_EXTENSION);
             $cacheFile = $config->get('lavanda.image_cache_path') . DIRECTORY_SEPARATOR . crc32($file) . $width . $height . '.' . $ext;
             if (!file_exists($cacheFile) || filemtime($file) > filemtime($cacheFile)) {
                 list($imgWidth, $imgHeight, $type) = getimagesize($file);
                 $image = imageOpen($type, $file);
                 if ($width && !$height) {
                     if ($width != $imgWidth) {
                         $ratio = $width / $imgWidth;
                         $height = $imgHeight * $ratio;
                     } else {
                         $height = $imgHeight;
                     }
                 }
                 if ($height && !$width) {
                     if ($height != $imgHeight) {
                         $ratio = $height / $imgHeight;
                         $width = $imgWidth * $ratio;
                     } else {
                         $width = $imgWidth;
                     }
                 }
                 $cacheImage = imagecreatetruecolor($width, $height);
                 imagecopyresampled($cacheImage, $image, 0, 0, 0, 0, $width, $height, $imgWidth, $imgHeight);
                 imageSave($type, $cacheFile, $cacheImage, $compression);
             }
         }
         return response(file_get_contents($cacheFile))->header('Content-Type', mime_content_type($cacheFile));
     } else {
         abort(404);
     }
 }
示例#3
0
function imageResize($srcPath, $destPath, $destType, $destWidth, $destHeight, $sourceX	= 0, $sourceY	= 0, $currentWidth=0, $currentHeight=0)
{
	// See if we can grab image transparency
	$image				= imageOpen( $srcPath , $destType );
	$transparentIndex	= imagecolortransparent( $image );

	// Create new image resource
	$image_p			= ImageCreateTrueColor( $destWidth , $destHeight );
	$background			= ImageColorAllocate( $image_p , 255, 255, 255 ); 
	
	// Set the new image background width and height
	$resourceWidth		= $destWidth;
	$resourceHeight		= $destHeight;
	
	if(empty($currentHeight) && empty($currentWidth))
	{
		list($currentWidth , $currentHeight) = getimagesize( $srcPath );
	}
	// If image is smaller, just copy to the center
	$targetX = 0;
	$targetY = 0;

	// If the height and width is smaller, copy it to the center.
	if( $destType != 'image/jpg' &&	$destType != 'image/jpeg' && $destType != 'image/pjpeg' )
	{
		if( ($currentHeight < $destHeight) && ($currentWidth < $destWidth) )
		{
			$targetX = intval( ($destWidth - $currentWidth) / 2);
			$targetY = intval( ($destHeight - $currentHeight) / 2);
	
			// Since the 
	 		$destWidth = $currentWidth;
	 		$destHeight = $currentHeight;
		}
	}
	
	// Resize GIF/PNG to handle transparency
	if( $destType == 'image/gif' )
	{
		$colorTransparent = imagecolortransparent($image);
		imagepalettecopy($image, $image_p);
		imagefill($image_p, 0, 0, $colorTransparent);
		imagecolortransparent($image_p, $colorTransparent);
		imagetruecolortopalette($image_p, true, 256);
		imagecopyresized($image_p, $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth , $destHeight , $currentWidth , $currentHeight );
	}
	else if( $destType == 'image/png' || $destType == 'image/x-png')
	{
		// Disable alpha blending to keep the alpha channel
		imagealphablending( $image_p , false);
		imagesavealpha($image_p,true);
		$transparent		= imagecolorallocatealpha($image_p, 255, 255, 255, 127);
		
		imagefilledrectangle($image_p, 0, 0, $resourceWidth, $resourceHeight, $transparent);
		imagecopyresampled($image_p , $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth, $destHeight, $currentWidth, $currentHeight);
	}
	else
	{
		// Turn off alpha blending to keep the alpha channel
		imagealphablending( $image_p , false );
		imagecopyresampled( $image_p , $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth , $destHeight , $currentWidth , $currentHeight );
	}

	// Output
	ob_start();
	
	// Test if type is png
	if( $destType == 'image/png' || $destType == 'image/x-png' )
	{
		imagepng( $image_p );
	}
	elseif ( $destType == 'image/gif')
	{
		imagegif( $image_p );
	}
	else
	{
		// We default to use jpeg
		imagejpeg($image_p, null, 80);
	}
	
	$output = ob_get_contents();
	ob_end_clean();

	return JFile::write( $destPath , $output );
}