/**
  *	If sending an HTML message with embedded images, use this function
  *	to add the image.
  *
  *	@param $file	The image file path.
  *	@param $c_type	(optional) The content type of the image or file.
  *	@param $name	(optional) The filename of the image.
  */
 function addHTMLImage($file, $c_type = '', $name = '')
 {
     if (!YDObjectUtil::isSubClass($file, 'YDFSImage')) {
         $file = new YDFSImage($file);
     }
     $data = $file->getContents();
     if (empty($name)) {
         $name = $file->getBaseName();
     }
     if (empty($c_type)) {
         $c_type = $file->getMimeType();
     }
     $this->_msg->addHTMLImage($data, $name, $c_type);
 }
 /**
  *	This function will do the actual work of creating a thumbnail image.
  *
  *	@param $width	The maximum width of the thumbnail.
  *	@param $height	The maximum height of the thumbnail.
  *	@param $cache	(optional) Indicate if the thumbnails should be cached. By default, caching is turned off.
  *
  *	@internal
  */
 function &_createThumbnail($width, $height, $cache = true)
 {
     // Check if the GD library is loaded.
     if (!extension_loaded('gd')) {
         $this->_error('YD_gd_not_installed');
     }
     // Include phpThumb
     require_once 'phpThumb/phpthumb.class.php';
     // Create a new thumbnail object
     $thumb = new phpThumb();
     $thumb->src = $this->getAbsolutePath();
     // Set the options for the creation of thumbnails
     $thumb->config_nohotlink_enabled = false;
     $thumb->config_cache_directory = YD_DIR_TEMP;
     // Set the width and the height
     $thumb->w = $width;
     $thumb->h = $height;
     // Create the cached thumbnail
     $cacheFName = $thumb->GenerateCachedFilename();
     $cacheFName .= $this->getLastModified();
     $cacheFName .= $this->getAbsolutePath();
     $cacheFName = YD_TMP_PRE . 'N_' . md5($cacheFName) . '.tmn';
     $cacheFName = YD_DIR_TEMP . '/' . $cacheFName;
     // Check if caching is enabled
     if ($cache == true) {
         // Output the cached version if any
         if (is_file($cacheFName)) {
             $img = new YDFSImage($cacheFName);
             header('Content-type: ' . $img->getMimeType());
             echo $img->getContents();
             die;
         }
     }
     // Width should be positive integer
     if ($width < 1) {
         $this->_error();
     }
     // Height should be positive integer
     if ($width < 1) {
         $this->_error();
     }
     // Generate the thumbnail
     $thumb->GenerateThumbnail();
     // Check if caching is enabled
     if ($cache == true) {
         $thumb->RenderToFile($cacheFName);
     }
     // Return the thumbnail object
     return $thumb;
 }
 /**
  *	This function will do the actual work of creating a thumbnail image.
  *
  *	@param $width	The maximum width of the thumbnail.
  *	@param $height	The maximum height of the thumbnail.
  *	@param $cache	(optional) Indicate if the thumbnails should be cached. By default, caching is turned off.
  *	@param $crop	(optional) Indicate if the thumbnails should be cropped to the exact size. By default, false.
  *
  *	@internal
  */
 function &_createThumbnail($width, $height, $cache = true, $crop = false)
 {
     // Check if the GD library is loaded.
     if (!extension_loaded('gd')) {
         $this->_error('YD_gd_not_installed');
     }
     // Width and height should be positive integer
     if ($width < 1 || $height < 1) {
         $this->_error();
     }
     // Get the cache filename
     $cacheFName = YD_DIR_TEMP . '/' . $this->_createThumbnailName($width, $height);
     // Check if caching is enabled
     if ($cache === true) {
         // Output the cached version if any
         if (is_file($cacheFName)) {
             // Create a new image for the cache file
             $img = new YDFSImage($cacheFName);
             // Set the content type
             header('Content-type: ' . $img->getMimeType());
             // Output the image
             readfile($cacheFName);
             die;
         }
     }
     // Check the extension
     $img_type = $this->isImage();
     // Open the source image
     if ($img_type == 'gif') {
         if (!function_exists('imagecreatefromgif')) {
             $this->_error();
         }
         $src_img = imagecreatefromgif($this->getAbsolutePath());
     } elseif ($img_type == 'png') {
         $src_img = imagecreatefrompng($this->getAbsolutePath());
     } else {
         $src_img = imagecreatefromjpeg($this->getAbsolutePath());
     }
     // Get the current image size
     $ori_width = imageSX($src_img);
     $ori_height = imageSY($src_img);
     // Calculate the new image size
     if ($crop) {
         if ($ori_width > $ori_height) {
             $thumb_w = ceil($ori_width * ($height / $ori_height));
             $thumb_h = $height;
         }
         if ($ori_width < $ori_height) {
             $thumb_w = $width;
             $thumb_h = ceil($ori_height * ($width / $ori_width));
         }
     } else {
         if ($ori_width > $ori_height) {
             $thumb_w = $width;
             $thumb_h = ceil($ori_height * ($width / $ori_width));
         }
         if ($ori_width < $ori_height) {
             $thumb_w = ceil($ori_width * ($height / $ori_height));
             $thumb_h = $height;
         }
     }
     if ($ori_width == $ori_height) {
         $thumb_w = $width;
         $thumb_h = $height;
     }
     if (($width >= $ori_width || $height >= $ori_height) && (!$crop || $crop && YDConfig::get('YD_FS_CROP') != YD_FS_CROP_ENLARGED)) {
         if ($width >= $ori_width && $height < $ori_height) {
             $thumb_w = ceil($ori_width * ($height / $ori_height));
             $thumb_h = $height;
         } else {
             if ($width < $ori_width && $height >= $ori_height) {
                 $thumb_w = $width;
                 $thumb_h = ceil($ori_height * ($width / $ori_width));
             } else {
                 $thumb_w = $ori_width;
                 $thumb_h = $ori_height;
             }
         }
     }
     // Resample the image
     $dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
     if ($img_type == 'png') {
         imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $ori_width, $ori_height);
     } else {
         imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $ori_width, $ori_height);
     }
     if ($crop && ($width != $thumb_w || $height != $thumb_h)) {
         $x = ceil(abs($thumb_w - $width) / 2);
         $y = ceil(abs($thumb_h - $height) / 2);
         $default = true;
         if ($ori_width < $width || $ori_height < $height) {
             switch (YDConfig::get('YD_FS_CROP', YD_FS_CROP_ENLARGED)) {
                 case YD_FS_CROP_UNCHANGED:
                     if ($ori_width < $width && $ori_height < $height) {
                         $crp_img = $dst_img;
                         $default = false;
                     } else {
                         if ($ori_width < $width) {
                             $x = 0;
                             $width = $ori_width;
                         } else {
                             if ($ori_height < $height) {
                                 $y = 0;
                                 $height = $ori_height;
                             }
                         }
                     }
                     break;
                 case YD_FS_CROP_ENLARGED:
                 case YD_FS_CROP_BORDERED:
                     break;
             }
         }
         if ($default) {
             $crp_img = imagecreatetruecolor($width, $height);
             if ($img_type == 'png') {
                 imagecopyresized($crp_img, $dst_img, 0, 0, $x, $y, $width, $height, $width, $height);
             } else {
                 imagecopyresampled($crp_img, $dst_img, 0, 0, $x, $y, $width, $height, $width, $height);
             }
         }
         $dst_img = $crp_img;
     }
     // Get the resulting image
     ob_start();
     if ($img_type == 'gif') {
         if (!function_exists('imagegif')) {
             imagepng($dst_img);
         } else {
             imagegif($dst_img);
         }
     } elseif ($img_type == 'png') {
         imagepng($dst_img);
     } else {
         imagejpeg($dst_img);
     }
     $image_data = ob_get_contents();
     ob_end_clean();
     // Destroy the images
     imagedestroy($dst_img);
     imagedestroy($src_img);
     // Save the cache if needed
     if ($cache == true) {
         $f = new YDFSFile($cacheFName, true);
         $f->setContents($image_data);
     }
     // Return the image data
     return $image_data;
 }
 /**
  *	If sending an HTML message with embedded images, use this function
  *	to add the image.
  *
  *	@param $file	The image file path.
  *	@param $c_type	(optional) The content type of the image or file.
  *	@param $name	(optional) The filename of the image.
  */
 function addHTMLImage($file, $c_type = '', $name = '')
 {
     if (!YDObjectUtil::isSubClass($file, 'YDFSImage')) {
         $file = new YDFSImage($file);
     }
     if (empty($name)) {
         $name = $file->getBaseName();
     }
     if (empty($c_type)) {
         $c_type = $file->getMimeType();
     }
     $this->_msg->AddEmbeddedImage($file->getAbsolutePath(), $name, $name, 'base64', $c_type);
 }
 /**
  *	This function will do the actual work of creating a thumbnail image.
  *
  *	@param $width	The maximum width of the thumbnail.
  *	@param $height	The maximum height of the thumbnail.
  *	@param $cache	(optional) Indicate if the thumbnails should be cached. By default, caching is turned off.
  *
  *	@internal
  */
 function &_createThumbnail($width, $height, $cache = true)
 {
     // Check if the GD library is loaded.
     if (!extension_loaded('gd')) {
         $this->_error('YD_gd_not_installed');
     }
     // Width and height should be positive integer
     if ($width < 1 || $width < 1) {
         $this->_error();
     }
     // Get the cache filename
     $cacheFName = YD_DIR_TEMP . '/' . $this->_createThumbnailName($width, $height);
     // Check if caching is enabled
     if ($cache === true) {
         // Output the cached version if any
         if (is_file($cacheFName)) {
             // Create a new image for the cache file
             $img = new YDFSImage($cacheFName);
             // Set the content type
             header('Content-type: ' . $img->getMimeType());
             // Output the image
             readfile($cacheFName);
             die;
         }
     }
     // Check the extension
     $img_type = $this->isImage();
     // Open the source image
     if ($img_type == 'gif') {
         if (!function_exists('imagecreatefromgif')) {
             $this->_error();
         }
         $src_img = imagecreatefromgif($this->getAbsolutePath());
     } elseif ($img_type == 'png') {
         $src_img = imagecreatefrompng($this->getAbsolutePath());
     } else {
         $src_img = imagecreatefromjpeg($this->getAbsolutePath());
     }
     // Get the current image size
     $ori_width = imageSX($src_img);
     $ori_heigth = imageSY($src_img);
     // Calculate the new image size
     if ($width >= $ori_width || $height >= $ori_heigth) {
         $thumb_w = $ori_width;
         $thumb_h = $ori_heigth;
     } else {
         if ($ori_width > $ori_heigth) {
             $thumb_w = $width;
             $thumb_h = $ori_heigth * ($width / $ori_width);
         }
         if ($ori_width < $ori_heigth) {
             $thumb_w = $ori_width * ($height / $ori_heigth);
             $thumb_h = $height;
         }
         if ($ori_width == $ori_heigth) {
             $thumb_w = $width;
             $thumb_h = $height;
         }
     }
     // Resample the image
     $dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
     if ($img_type == 'png') {
         imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $ori_width, $ori_heigth);
     } else {
         imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $ori_width, $ori_heigth);
     }
     // Get the resulting image
     ob_start();
     if ($img_type == 'gif') {
         if (!function_exists('imagegif')) {
             imagepng($dst_img);
         } else {
             imagegif($dst_img);
         }
     } elseif ($img_type == 'png') {
         imagepng($dst_img);
     } else {
         imagejpeg($dst_img);
     }
     $image_data = ob_get_contents();
     ob_end_clean();
     // Destroy the images
     imagedestroy($dst_img);
     imagedestroy($src_img);
     // Save the cache if needed
     if ($cache == true) {
         $f = new YDFSFile($cacheFName, true);
         $f->setContents($image_data);
     }
     // Return the image data
     return $image_data;
 }
 /**
  *	This function is used to output an error image.
  *
  *	@param $name	(optional) Name of the error image. Default image that is shown is the generic
  *					"YD_ydfsimage_fatal_error".
  *
  *	@internal
  */
 function _error($name = 'YD_ydfsimage_fatal_error')
 {
     $img = new YDFSImage(YD_DIR_HOME . '/images/' . $name . '.gif');
     header('Content-type: ' . $img->getMimeType());
     echo $img->getContents();
     die;
 }