function actionThumbnailSave() { @unlink('fsimage.thumb.jpg'); $img = new YDFSImage('fsimage.jpg'); $img->saveThumbnail(150, 110, 'fsimage.thumb.jpg'); header('Location: fsimage.thumb.jpg'); }
function actionAdd() { // We return to the default action $action = 'default'; // Process the form if ($this->form->validate()) { // Move the uploaded file $file = $this->form->getElement('image'); if ($file->isUploaded()) { // Get the new filename $filename = YDStringUtil::stripSpecialCharacters($file->getBaseName()); // Move the upload $file->moveUpload($this->dir_rel, $filename); // Check if it's an image $fileObj = new YDFSFile($this->dir_rel . $file->getBaseName()); if (!$fileObj->isImage()) { @unlink($this->dir_rel . $file->getBaseName()); } // Create the thumbnails $thumb = new YDFSImage($this->dir_rel . $file->getBaseName()); $thumb->_createThumbnail(100, 100, true); $thumb->_createThumbnail(48, 48, true); } // Get the name of the action $action = $this->form->getValue('action'); } // Redirect to the list view $destination = YD_SELF_SCRIPT . '?do=' . $action; if (isset($_GET['field'])) { $destination .= '&field=' . $_GET['field']; } $this->redirect($destination); }
function actionThumbnailSmall() { // Get the image name if (!isset($_GET['id'])) { die('No image selected.'); } // Create a new image object $img = new YDFSImage(YDConfig::get('dir_uploads', '../uploads') . '/' . $_GET['id']); // Output the thumbnail $img->outputThumbnail(48, 48); }
/** * 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; }
function resizeUploadedImage($image) { // Convert the YDFSFile objects to images if (is_object($image) && strtolower(get_class($image)) == 'ydfsfile') { $image = new YDFSImage($image->getAbsolutePath()); } // Convert strings to images if (is_string($image)) { $image = new YDFSImage($image); } // Get the maximum X and Y size $max_x = YDConfig::get('max_img_size_x', ''); $max_y = YDConfig::get('max_img_size_y', ''); $max_x = empty($max_x) ? 999999 : $max_x; $max_y = empty($max_y) ? 999999 : $max_y; // Do nothing if maximum sizes are specified if ($max_x == 99999 && $max_y == 999999) { return $image; } // Resize the image $image->saveThumbnail($max_x, $max_y, $image->getAbsolutePath()); // Return the image return $image; }
/** * 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); }
function actionThumbnailSmall() { if (!isset($_GET['id'])) { die('No image selected.'); } $img = new YDFSImage(YDConfig::get('dir_uploads', '../uploads') . '/' . $_GET['id']); $img->outputThumbnail(48, 48); }
/** * 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 exports the image */ function create() { // add simbols ;) // don't add possible character problems for user, eg, l <--> 1 (lower L or number ONE?), 0 <--> O (ZERO OR upper O?) switch ($ch = YDConfig::get('YD_CAPTCHA_CHARSET')) { case 'simple': $this->_img->SetCharSet("a-h,6,2-4,8,j-k,m-n,p-r,2-4,6,8-9,t-w,y-z,A-H,J-K,M-N,6,2-4,8,9,P-R,T-W,Y-Z,2-4,6,8-9"); break; case 'complex': $this->_img->SetCharSet("a-h,!,j-k,#,&,%,\$,m-n,@,p-r,2-4,6,£,?,8-9,t-w,y-z,#,&,%,\$,A-H,!,J-K,#,&,%,\$,M-N,@,P-R,T-W,Y-Z,2-4,6,£,?,8-9"); break; case 'letters': $this->_img->SetCharSet("a-h,j-k,J-K,M-N,m-n,p-r,t-w,y-z,A-H,J-K,M-N,P-R,p-r,t-w,T-W,Y-Z"); break; case 'numeric': $this->_img->SetCharSet("2-4,6,6,3,2,8-9,2-4,6,8-9,8-9,6,2-4,9,7,8,6,3,2"); break; default: $this->_img->SetCharSet($ch); } $this->_img->DisplayShadow(YDConfig::get('YD_CAPTCHA_SHADOW')); $this->_img->useColour(YDConfig::get('YD_CAPTCHA_COLOR')); $this->_img->SetNumChars(YDConfig::get('YD_CAPTCHA_NUMCHARS')); // apply image color $bgcolor = YDConfig::get('YD_CAPTCHA_BGCOLOR'); $this->_img->_red = hexdec(substr($bgcolor, 1, 2)); $this->_img->_green = hexdec(substr($bgcolor, 3, 2)); $this->_img->_blue = hexdec(substr($bgcolor, 5, 2)); if ($this->_img->Create() == false) { YDInclude('YDFilesystem.php'); YDFSImage::_error('YD_gd_not_installed'); } }
/** * 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; }