Location: PHPKode > projects > FUNKWERK > apifunk/inc/class.pictures.inc.php
<?php
	/*******************************************************************\
	* FUNKWERK - Pictures                                               *
	* http://www.funkwerk.net                                           *
	*                                                                   *
	* Manipulate images using GD and ImageMagick                        *
	*                                                                   *
	* Written by by Bettina Gille [hide@address.com]                    *
	*           and Miles Lott <hide@address.com>               *
	*                                                                   *
	* Copyright (C) 2002 Bettina Gille                                  *
	* ------------------------------------------------------------------*
	* Some methods based on OIC v1.0.6 - the Online Image Converter     *
	* Copyright (C) 2001 Ray Lopez (http://www.TheDreaming.com)         *
	* ------------------------------------------------------------------*
	* This library is part of the FUNKWERK API                          *
	* ------------------------------------------------------------------*
	* This library is free software; you can redistribute it and/or     *
	* modify it under the terms of the GNU General Public License as    *
	* published by the Free Software Foundation; either version 2 of    *
	* the License, or (at your option) any later version.               *
	*                                                                   *
	* This program is distributed in the hope that it will be useful,   *
	* but WITHOUT ANY WARRANTY; without even the implied warranty of    *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  *
	* General Public License for more details.                          *
	*                                                                   *
	* You should have received a copy of the GNU General Public License *
	* along with this program; if not, write to the Free Software       *
	* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         *
	\*******************************************************************/
	/* $Id: class.pictures.inc.php,v 1.4 2002/08/29 03:47:17 bettina Exp $ */

	class pictures
	{
		var $debug = False;
		var $tmpfile = '';
		var $tmpdir = '';
		var $filename;
		var $convertcmd = '';
		var $convert_select;
		var $angles;
		var $icr = '';

		function pictures($debug=False)
		{
			if(!function_exists('imagecreate'))
			{
				echo '<br><center>' . lang('No GD support in this PHP install !') . '</center><br><br>';
			}

			$this->debug			= $debug;
			$this->angles			= array(lang('Select'),'90','180','270');
			$this->convert_select	= array('0' => lang('Select'),
											'1'	=> 'GIF',
											'2'	=> 'JPG',
											'3' => 'PNG',
											'5'	=> 'WBMP');

			$this->tmpdir			= $GLOBALS['phpgw_info']['server']['temp_dir'] . SEP;
			$this->appname			= $GLOBALS['phpgw_info']['flags']['currentapp'];
			$this->urlbase			= $GLOBALS['phpgw_info']['server']['webserver_url'] . SEP . $this->appname . SEP . 'images' . SEP;
			$this->docroot			= $GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'];
			$this->img_dir			= PHPGW_APP_ROOT . SEP . 'images' . SEP;

			$this->config			= CreateObject('phpgwapi.config','apifunk_pix');
			$this->config->read_repository();

			$this->config_data		= $this->config->config_data;

			/*
		  	This is the function to be used for resize, etc.
		 	 A check is done in the constructor for the nicer function available in gd 2.X.
		  	Since there is no way to really check, make sure your gd version is correct in admin.
			*/

			if($this->config_data['gd_version'] >= 2)
			{
				$this->icr = 'imagecopyresampled';
			}
			else
			{
				$this->icr = 'imagecopyresized';
			}

			/*
		  	This setting is used to call convert.  This is the default.
		  	Used for rotate, and for conversion if no internal GIF support available.
			*/

			$this->convertcmd = $this->config_data['convert_cmd'];
		}

		function save_config($values)
		{
			if (is_array($values))
			{
				if ($values['ttf'])
				{
					$values['ttf'] = 'yes';
				}
				else
				{
					$values['ttf'] = 'no';
				}

				while (list($key,$config) = each($values))
				{
					if ($config)
					{
						$this->config->config_data[$key] = $config;
					}
					else
					{
						unset($config->config_data[$key]);
					}
				}
				$this->config->save_repository(True);
			}
		}

		function check_config($per = 'adm')
		{
			switch ($per)
			{
				case 'adm':
					if (!imagetypes())
					{
						$error[] = lang('The php version you are using does not support the GD graphics library !<br>Without GD the FUNKWERK applications will not work !');
					}
					break;
				case 'user':
					if (!isset($this->config_data['convert_cmd']) || !isset($this->config_data['gd_version']))
					{
						$error[] = lang('Your system is not configured yet ! Please contact the administrator !');
					}
					break;
			}

			if (is_array($error))
			{
				return $error;
			}
		}

		/*
		  Note, GD supports WBMP. This is not the Windoze bitmap format, but
		  the monochrome bitmaps used in PDAs, etc.
		  */

		function get_image_size_array($img_file = '')
		{
			if ($img_file)
			{
				$imgSizeArray = @GetImageSize($img_file);

				if (is_array($imgSizeArray))
				{
					$img_data = array
					(
						'width'		=> $imgSizeArray[0],
						'height'	=> $imgSizeArray[1],
						'type'		=> $imgSizeArray[2]
					);

					switch($imgSizeArray[2])
					{
						case '1': $img_data['img_type'] = 'GIF'; break;
						case '2': $img_data['img_type'] = 'JPG'; break;
						case '3': $img_data['img_type'] = 'PNG'; break;
						case '4': $img_data['img_type'] = 'SWF'; break;
					}

					if ($imgSizeArray[2] == 4)
					{
						$img_data = 'The SWF format is not supported !';
					}
				}
				else
				{
					$wbmp = @imageCreateFromWBMP($img_file);
					if ($wbmp && $wbmp != '')
					{
						$img_data['width']		= imagesx($wbmp);
						$img_data['height']		= imagesy($wbmp);
						$img_data['type']		= '5';
						$img_data['img_type']	= 'BMP';
					}
					else
					{
						$img_data = 'Create image from WBMP failed !';
					}
				}
			}
			else
			{
				$img_data = 'No image file !';
			}
			return $img_data;
		}

		function get_img_type($img_file)
		{
			$img_data = $this->get_image_size_array($img_file);
			if($this->debug)
			{
				echo '<br>img->img_type(): ' . $img_file . "\n";
				_debug_array($img_data);
			}

			if ($img_data == 'none')
			{
				return $img_data;
			}
			else
			{
				return $img_data['type'];
			}
		}

		function smartcopy($src,$dest)
		{
			$src_type  = $this->get_img_type($src);
			$dest_type = $this->get_img_type($dest);
			if($this->debug)
			{
				echo '<br>smartcopy()' . "\n";
				echo '<br>Source file: ' . $src . "\n";
				echo '<br>Target file: ' . $dest . "\n";
				echo '<br>Source type: ' . $srctype . "\n";
				echo '<br>Target type: ' . $desttype . "\n";
			}

			if($src_type == $dest_type)
			{
				copy($src,$dest);
				unlink($src);
				return $dest;
			}
			else
			{
				switch ($src_type)
				{
					case '1':
						$ext = 'gif';
						break;
					case '2':
						$ext = 'jpg';
						break;
					case '3':
						$ext = 'png';
						break;
					case '5':
						$ext = 'bmp';
				}
				switch ($dest_type)
				{
					case '1':
						$oldext = 'gif';
						break;
					case '2':
						$oldext = 'jpg';
						break;
					case '3':
						$oldext = 'png';
						break;
					case '5':
						$oldext = 'bmp';
				}
				$newdest = ereg_replace("\.$oldext",".$ext",$dest);
				if($this->debug)
				{
					echo '<br>Old ext: ' . $oldext . "\n";
					echo '<br>New ext: ' . $ext . "\n";
					echo '<br>NEW Target file: ' . $newdest . "\n";
				}
				copy($src,$newdest);
				unlink($src);
				return $newdest;
			}
		}

		/* Save the temporary file into the document tree, or wherever */
		function save($imgfile,$dest='',$extra='')
		{
			$old_imgfile	= $imgfile;
			$imgfile		= ereg_replace($this->tmpdir,'',$imgfile);

			if($dest)
			{
				$dest = ereg_replace($this->tmpdir,'',$dest);
				$destfile = $dest;
				$dest = $this->img_dir . $destfile . $extra;
			}
			else
			{
				$destfile = $imgfile;
				$dest = $this->img_dir . $destfile . $extra;
			}

			$url = $this->urlbase . $destfile . $extra;

			if($this->debug)
			{
				echo '<br>img->save(): ' . $this->tmpdir . $imgfile . ' as ' . $dest;
			}

			copy($this->tmpdir . $imgfile,$dest);

			unlink($old_imgfile);

			return $url;
		}

		/* Create a tmp filename */
		function tmp_file($name='phpgwpic_')
		{
			srand((double)microtime()*1000000);
			return($this->tmpdir . $name . rand(1,100000));
		}

		function image_from_gif($img_file)
		{
			if (imagetypes() & IMG_GIF)
			{
				$im = imageCreateFromGIF($img_file);
			}
			else
			{
				$newtmp = $this->convert($img_file,'png');
				if($this->debug)
				{
					echo '<br>uiiimg->manipulate($convert): called img->convert('
						. $img_file  . ',png) to create: '
						. $newtmp;
				}
				$im = imageCreateFromPNG($newtmp);
			}
			return $im;
		}

		function image_from_jpeg($img_file)
		{
			return imageCreateFromJPEG($img_file);
		}

		function image_from_png($img_file)
		{
			return imageCreateFromPNG($img_file);
		}

		function image_from_wbmp($img_file)
		{
			return imageCreateFromWBMP($img_file);
		}

		/* format conversion routines */
		function image2jpg($im,$dest='')
		{
			if (imagetypes() & IMG_JPG)
			{
				$filename = $this->tmp_file() . '.jpg';
				imagejpeg($im, $filename);
				if($this->debug)
				{
					echo '<br>img->image2jpg(): calling img->save(' . $filename . ',' . $dest . ')';
				}
				return $this->save($filename,$dest);
			}
		}

		function image2gif($im,$dest='')
		{
			if (imagetypes() & IMG_GIF)
			{
				$filename = $this->tmp_file() . '.gif';
				imagegif($im, $filename);
				if($this->debug)
				{
					echo '<br>img->image2gif(): calling img->save(' . $filename . ',' . $dest . ')';
				}
				return $this->save($filename,$dest);
			}
			else
			{
				$filename = $this->tmp_file() . '.png';
				imagepng($im, $filename);

				/* Try to call convert to do this, maybe it can handle gif */
				if($this->debug)
				{
					echo '<br>img->image2gif: calling img->_convert(' . $filename . ",''," . 'gif)';
				}
				return $this->_convert($filename,'','gif');
			}
		}

		function image2wbmp($im,$dest='')
		{
			if (imagetypes() & IMG_WBMP)
			{
				$filename = $this->tmp_file() . '.bmp';
				imagewbmp($im, $filename,0);
				if($this->debug)
				{
					echo '<br>img->image2bmp(): calling img->save(' . $filename . ',' . $dest . ')';
				}
				return $this->save($filename,$dest);
			}
		}

		function image2png($im,$dest='')
		{
			if (imagetypes() & IMG_PNG)
			{
				$filename = $this->tmp_file() . '.png';
				imagepng($im, $filename);
				if($this->debug)
				{
					echo '<br>img->image2png(): calling img->save(' . $filename . ',' . $dest . ')';
				}
				return $this->save($filename,$dest);
			}
		}

		function trim_name($img_name)
		{
			$ext = strstr($img_name,".");
			if ($ext)
			{
				$_name = explode(".",$img_name);
				return $_name[0];
			}

			$ext = strstr($img_name,"/");
			if ($ext)
			{
				$_name = explode("/",$img_name);
				return $_name[0];
			}
			return $img_name;
		}

		function save_image($img_file)
		{
			if($img_file['tmp_name'] || $img_file['name'])
			{
				if($img_file['tmp_name'] == 'none' || empty($img_file['tmp_name']))
				{
					return 'No image';
					break;
				}
				if($img_file['name'] == 'none' || empty($img_file['name']))
				{
					return 'No image';
					break;
				}

				$img_data = $this->get_image_size_array($img_file['tmp_name']);

				if (!is_array($img_data))
				{
					return $img_data;
					break;
				}

				if($this->debug)
				{
					echo '<br>uiiimg->add(): called getimagesize(' . $img_file['tmp_name'] . ')';
					_debug_array($img_data);
				}

				if (is_array($img_data))
				{
					switch ($img_data['type'])
					{
						case '1':
						// GIF
						if (imagetypes() & IMG_GIF)
						{
							$im		= imagecreatefromgif($img_file['tmp_name']);
							$image	= $this->image2gif($im,$img_file['name']);
						}
						else
						{
							$tmp_img = ereg_replace('/tmp/',$this->tmpdir,$img_file['tmp_name']);
							copy($img_file['tmp_name'],$tmp_img . '.gif');

							$newtmp = $this->_convert($tmp_img . '.gif',$tmp_img . '.png','png');

							if($this->debug)
							{
								echo '<br>uiiimg->add(): called img->_convert(' . $tmp_img . '.gif,'
									. $tmp_img . '.png,png) to create: ' . $newtmp;
							}
							//unset($newtmp);
							$img_file['name'] = ereg_replace("\.gif",'.png',$img_file['name']);
							@unlink($tmp_img . '.gif');
							$im = $this->image_from_png($this->docroot . $newtmp);
							@unlink($tmp_img . '.png');
							$image = $this->image2png($im,$img_file['name']);
							$img_data['img_type'] = 'PNG';
						}
						break;
						case '2':
						//JPG
							$im		= $this->image_from_jpeg($img_file['tmp_name']);
							$image	= $this->image2jpg($im,$img_file['name']);
						break;
						case '3':
						//PNG
							$im		= $this->image_from_png($img_file['tmp_name']);
							$image	= $this->image2png($im,$img_file['name']);
						break;
						case '5':
						//WBMP
							$im		= $this->image_from_wbmp($img_file['tmp_name']);
							$image	= $this->image2wbmp($im,$img_file['name']);
					}

					unlink($img_file['tmp_name']);
					return array('image' => $image,'thumb' => $this->thumbnail($im,$img_data['width'],$img_data['height'],100,
								strtolower($img_data['img_type']),$this->trim_name($img_file['name'])),'name' => $img_file['name']);
				}
				else
				{
					return $img_data;
				}
			}
		}

		/* Create a scaled thumbnail image */
		function thumbnail($im,$width,$height,$dstW=100,$format='jpg',$im_name = '')
		{
			if (! $im_name)
			{
				$im_name = 'phpgwpic_thumb_';
			}
			else
			{
				$im_name = $im_name . '_thumb_';
			}

			$filename = $this->tmp_file($im_name) . '.' . $format;
			$dstH = $this->_get_thumbsize($width,$height,$dstW);

			/* Create a new image handle */
			$newim = imagecreate($dstW,$dstH);
			if($this->debug)
			{
				echo '<br>img->thumbnail(): calling img->_resize($newim,$im,"'
					. $dstX . '","' . $dstY . '","' . $srcX . '","' . $srcY . '","'
					. $dstW . '","' . $dstH . '","' . $width . '","' . $height
					. '","' . $filename . '","' . $format . '")';
			}
			return ($this->_resize($newim,$im,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$width,$height,$filename,$format));
		}

		/* Resize an image */
		function resize($im,$width,$height,$dest_width,$dest_height,$format='png')
		{
			$filename = $this->tmp_file();
			$newim = imagecreate($dest_width,$dest_height);
			if($this->debug)
			{
				echo '<br>img->resize(): calling img->_resize($newim,$im,0,0,0,0,"'
					. $dstW . '","' . $dstH . '","' . $width . '","' . $height
					. '","' . $filename . '","' . $format . '")';
			}
			return ($this->_resize($newim,$im,0,0,0,0,$dest_width,$dest_height,$width,$height,$filename,$format));
		}

		/* Crop an image using upper left corner and size attributes */
		function crop($im,$width,$height,$dstW,$dstH,$srcX,$srcY,$format='png')
		{
			$filename = $this->tmp_file() . '.' . $format;
			$newim = imagecreate($dstW,$dstH);

			$cmd = $this->icr . '($newim,$im,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$width,$height);';
			eval($cmd);
			if($this->debug)
			{
				echo '<br>img->crop(): calling img->_resize($newim,$im,"'
					. $dstX . '","' . $dstY . '","' . $srcX . '","' . $srcY . '","'
					. $dstW . '","' . $dstH . '","' . $width . '","' . $height
					. '","' . $filename . '","' . $format . '")';
			}
			return ($this->_resize($newim,$im,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$width,$height,$filename,$format));
		}

		/* This one is special.  It uses the convert command (part of ImageMagick). */
		function rotate($im, $angle=90, $img_type, $im_name = '')
		{
			if (! $im_name)
			{
				$im_name = 'phpgwpic_rot_';
			}
			else
			{
				$im_name = $im_name . '_rot_';
			}

			if($this->convertcmd)
			{
				$tmpfile = $this->tmp_file($im_name);

				switch ($img_type)
				{
					case '1':
						if (imagetypes() & IMG_GIF)
						{
							imagegif($im, $tmpfile);
							$ext = '.gif';
						}
						else
						{
							imagepng($im, $tmpfile);
							$ext = '.png';
						}
						break;
					case '2':
						if (imagetypes() & IMG_JPG)
						{
							imagejpeg($im, $tmpfile);
							$ext = '.jpg';
						}
						break;
					case '3':
						if (imagetypes() & IMG_PNG)
						{
							imagepng($im, $tmpfile);
							$ext = '.png';
						}
						break;
					case '5':
						if (imagetypes() & IMG_WBMP)
						{
							imagewbmp($im, $tmpfile);
							$ext = '.bmp';
						}
						break;
				}

				if(@stat($tmpfile))
				{
					$tmp = $this->tmp_file($im_name);
					$cmd = $this->convertcmd . ' ' . $tmpfile . ' -rotate ' . $angle . ' ' . $tmp;
					if($this->debug)
					{
						echo '<br>img->rotate(): ' . $cmd;
					}
					passthru($cmd);
					if($this->debug)
					{
						echo '<br>img->rotate(): calling img->save(' . $tmp . ",''," . $ext . ')';
					}
					return $this->save($tmp,'',$ext);
				}
			}
		}

		/* Used mainly for gif support */
		function convert($src,$type)
		{
			$dest = ereg_replace("\.gif",".$type",$src);
			$cmd  = $this->convertcmd . ' ' . $src . ' ' . $dest;
			if($this->debug)
			{
				echo '<br>img->convert(): ' . $cmd;
			}
			passthru($cmd);

			return $dest;
		}

		function _convert($src,$dest='',$type='')
		{
			if($type)
			{
				if($dest)
				{
				//	$tmp = ereg_replace($this->tmpdir,'',$dest);
					$tmp = $dest;
				}
				else
				{
					$tmp = $this->tmp_file() . '.' . $type;
				}
				$cmd = $this->convertcmd . ' ' . $src . ' ' . $tmp;
				if($this->debug)
				{
					echo '<br>img->_convert(): ' . $cmd;
				}
				passthru($cmd);
				if($this->debug)
				{
					echo '<br>img->_convert(): calling img->save(' . $tmp . ')';
				}
				return $this->save($tmp,'');
			}
		}

		/* Called by crop() and thumbnail() to check valid formats, resize, and save an image */
		function _resize($newim,$im,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$width,$height,$filename,$format)
		{
			$cmd = $this->icr . '($newim,$im,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$width,$height);';
			eval($cmd);
			switch ($format)
			{
				case 'bmp':
					if (imagetypes() & IMG_WBMP)
					{
						imagewbmp($newim, $filename);
					}
					break;
				case 'gif':
					if (imagetypes() & IMG_GIF)
					{
						imagegif($newim, $filename);
					}
					else
					{
						imagepng($newim, $filename);
						copy ($filename,$filename . '.png');
						unlink($filename);
						if($this->debug)
						{
							echo '<br>img->_resize(): calling img->_convert(' . $filename . '.png' . ",''," . 'gif)';
						}
						$this->_convert($filename . '.png','','gif');
					}
					break;
				case 'jpg':
					if (imagetypes() & IMG_JPG)
					{
						imagejpeg($newim, $filename);
					}
					break;
				case 'png':
				default:
					if (imagetypes() & IMG_PNG)
					{
						imagepng($newim, $filename);
					}
			}
			if($this->debug)
			{
				echo '<br>img->_resize(): calling img->save(' . $filename . ')';
			}
			return $this->save($filename);
		}

		/* Calculate thumbnail image height using width and original image size */

		function _get_thumbsize($width,$height,$newW='100')
		{
			$mult  = $width / $newW;
			$newH  = round($height / $mult);
			return $newH;
		}
	}
?>
Return current item: FUNKWERK