/** * Executes ResizeImage action * * This action can be used into an ajax action to resize an image. This function requires * some parameters: * image : The name of the image to resize * imagePath : The full image path * imageWidth : The new image width * imageHeight : The new image height * imageType : The new image type. This parameter must be 1-GIF 2-JPG 3-PNG. Other * formats will be ignored * imageQuality : The new value for the image quality. Applies only to JPG images * * return A JSon header. */ public function executeResizeImage() { // Resizes the image $imagePath = w3sCommonFunctions::checkLastDirSeparator(sfConfig::get('app_images_path')); $imageEditor = new ImageEditor($this->getRequestParameter('image'), $imagePath); echo $this->getRequestParameter('imageWidth') . ' - ' . $this->getRequestParameter('imageHeight'); $imageEditor->resize($this->getRequestParameter('imageWidth'), $this->getRequestParameter('imageHeight')); // Retrieve the image type and verifies if the new image will be of the same type $imageAttributes = getimagesize($imagePath . $this->getRequestParameter('image')); $newType = $this->getRequestParameter('imageType') != $imageAttributes[2] ? $this->getRequestParameter('imageType') : 0; // Writes the new image and returns its name $newFileName = $imageEditor->outputFile($this->getRequestParameter('image'), $imagePath, $newType, $this->getRequestParameter('imageQuality')); // Produces the output header for ajax calling. $canvasImage = w3sClassImageManager::getImageAttributes($newFileName, $this->getRequestParameter('previewWidth'), $this->getRequestParameter('previewHeight'), $this->getRequestParameter('setCanvas')); //$output = '[["w3s_image_size", "' . $imageAttributes["size"] . '"],["w3s_image_preview", "' . $imageAttributes["htmlImage"] . '"], ["w3s_image_canvas", "' . $imageAttributes["canvasValue"] . '"],["w3s_start_width", "' . $imageAttributes["width"] . '"],["w3s_start_height", "' . $imageAttributes["height"] . '"]]'; $jsonImage = '[["w3s_image_preview", "' . $canvasImage["htmlImage"] . '"],'; $jsonImage .= '["w3s_editor_image_size", "' . $canvasImage["size"] . '"],'; $jsonImage .= '["w3s_editor_width", "' . $canvasImage["width"] . '"],'; $jsonImage .= '["w3s_editor_height", "' . $canvasImage["height"] . '"],'; $jsonImage .= '["w3s_editor_type_select", "' . $canvasImage["imageType"] . '"],'; $jsonImage .= '["w3s_editor_canvas", "' . $canvasImage["canvasValue"] . '"],'; $jsonImage .= '["w3s_editor_start_width", "' . $canvasImage["width"] . '"],'; $jsonImage .= '["w3s_editor_start_height", "' . $canvasImage["height"] . '"],'; $jsonImage .= '["w3s_ppt_htmlImage", "' . $canvasImage["htmlImage"] . '"],'; $jsonImage .= '["w3s_ppt_imageType", "' . $canvasImage["imageType"] . '"],'; $jsonImage .= '["w3s_ppt_size", "' . $canvasImage["size"] . '"],'; $jsonImage .= '["w3s_ppt_width", "' . $canvasImage["width"] . '"],'; $jsonImage .= '["w3s_ppt_height", "' . $canvasImage["height"] . '"]]'; $this->imagesList = w3sCommonFunctions::buildFilesList($imagePath, $newFileName, array('gif', 'jpg', 'jpeg', 'png')); /* if ($newFileName != $this->getRequestParameter('image')){ }*/ $this->getResponse()->setHttpHeader("X-JSON", '(' . $jsonImage . ')'); return sfView::HEADER_ONLY; }
public static function resize($strFileName = "", $intWidth = 100, $intHeight = 75, $intTemplate = RESIZE_EXACT, $intQuality = 75, $blnOverwrite = FALSE, $strName = "", $blnToScreen = FALSE, $blnGrayscale = FALSE) { $strReturn = $strName; if (empty($strReturn)) { if (!$blnOverwrite) { //*** Generate a random name. $strExtension = self::getExtension(basename($strFileName)); $strReturn = dirname($strFileName) . "/{$intWidth}x{$intHeight}_" . basename($strFileName, $strExtension) . "_" . strtotime("now") . "{$strExtension}"; } else { $strReturn = $strFileName; } } $objEditor = new ImageEditor(basename($strFileName), dirname($strFileName) . "/"); switch ($intTemplate) { case RESIZE_CROP: //*** Don't blow the image up if it is smaller then the destination. if ($intWidth > $objEditor->getWidth() && $intHeight > $objEditor->getHeight()) { //*** Skip the resize. } else { $destX = ($objEditor->getWidth() - $intWidth) / 2; $destY = ($objEditor->getHeight() - $intHeight) / 2; $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight); } break; case RESIZE_FIT_CROP: //*** Resize the image to fit into the boundary box. //*** Portrait source. $destWidth = $intWidth; $destHeight = $destWidth / $objEditor->getWidth() * $objEditor->getHeight(); if ($destHeight < $intHeight) { //*** Landscape source. $destHeight = $intHeight; $destWidth = $destHeight / $objEditor->getHeight() * $objEditor->getWidth(); } //*** Don't blow the image up if it is smaller then the destination. if (round($destWidth) > $objEditor->getWidth() && round($destHeight) > $objEditor->getHeight()) { //*** Skipt the resize. $destX = ($objEditor->getWidth() - $intWidth) / 2; $destY = ($objEditor->getHeight() - $intHeight) / 2; $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight); } else { $objEditor->resize(round($destWidth), round($destHeight)); $destX = ($objEditor->getWidth() - $intWidth) / 2; $destY = ($objEditor->getHeight() - $intHeight) / 2; $objEditor->crop(round($destX), round($destY), $intWidth, $intHeight); } break; case RESIZE_DISTORT: //*** Don't blow the image up if it is smaller then the destination. if ($intWidth > $objEditor->getWidth() && $intHeight > $objEditor->getHeight()) { //*** Skipt the resize. } else { $objEditor->resize($intWidth, $intHeight); } break; case RESIZE_EXACT: $destWidth = $intWidth; $destHeight = $destWidth / $objEditor->getWidth() * $objEditor->getHeight(); if ($destHeight > $intHeight) { //*** Landscape source. $destHeight = $intHeight; $destWidth = $destHeight / $objEditor->getHeight() * $objEditor->getWidth(); } //*** Don't blow the image up if it is smaller then the destination. if (round($destWidth) > $objEditor->getWidth() && round($destHeight) > $objEditor->getHeight()) { //*** Skipt the resize. } else { $objEditor->resize(round($destWidth), round($destHeight)); } break; } if ($blnGrayscale) { $objEditor->grayscale(); } if (!empty($strReturn)) { if ($blnToScreen) { //*** Return image. $objEditor->outputImage($intQuality); } else { //*** Write file to disk. $objEditor->outputFile(basename($strReturn), dirname($strReturn) . "/", $intQuality); } } return $strReturn; }
function change_image($nazwa, $katalog, $w, $h, $new_nazwa = "") { if (file_exists($katalog . "/" . $nazwa)) { $plik = $katalog . "/" . $nazwa; } else { $plik = ""; } if (strlen($plik) > 0 && ($w > 0 || $h > 0)) { list($old_w, $old_h) = getimagesize($plik); $image = new ImageEditor($nazwa, $katalog); if ($w > 0 && $h == 0) { $new_w = $w; $new_h = ceil($new_w * $old_h / $old_w); $image->resize($new_w, $new_h); } else { if ($h > 0 && $w == 0) { $new_h = $h; $new_w = ceil($new_h * $old_w / $old_h); $image->resize($new_w, $new_h); } else { if ($h > 0 && $w > 0) { $image->resize($w, $h); } } } if (strlen($new_nazwa) > 0) { $image->outputFile($new_nazwa, $katalog . "/"); chmod($katalog . "/" . $new_nazwa, 0644); } else { $image->outputFile($nazwa, $katalog . "/"); chmod($katalog . "/" . $nazwa, 0644); } } }
function tb_post_thumb($generate = false, $alt_text = '', $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0, $entry = null) { global $post; if ($entry) { $post = $entry; } $settings = get_option('post_thumbnail_settings'); if ($resize_width) { $settings['resize_width'] = $resize_width; } if ($resize_height) { $settings['resize_height'] = $resize_height; } // find an image from your domain if (preg_match('/<img (.*?)src="https?:\\/\\/(www\\.)?' . str_replace('/', '\\/', $settings['domain_name']) . '\\/(.*?)"/i', $post->post_content, $matches)) { // put matches into recognizable vars // fix later, assumes document root will match url structure $the_image = $settings['base_path'] . '/' . $matches[3]; // check if image exists on server // if doesn't exist, can't do anything so return default image if (!file_exists($the_image)) { return tb_post_thumb_gen_image(str_replace($settings['full_domain_name'], $settings['base_path'], $settings['default_image']), $settings['default_image'], $alt_text, $generate); } $dest_path = pathinfo($the_image); // dir to save thumbnail to $save_dir = $dest_path['dirname'] . "/{$settings['folder_name']}"; // name to save to $filename_suffix = $resize_width . 'x' . $resize_height . '-' . $crop_x . '-' . $crop_y; $filename = substr($dest_path['basename'], 0, strrpos($dest_path['basename'], ".")); if ($settings['append'] == 'true') { $rename_to = $filename . $settings['append_text'] . '-' . $filename_suffix . '.' . $dest_path['extension']; } else { $rename_to = $settings['append_text'] . $filename . '-' . $filename_suffix . '.' . $dest_path['extension']; } // check if file already exists // return location if does if (file_exists($save_dir . '/' . $rename_to)) { $imagelocation = str_replace($settings['base_path'], $settings['full_domain_name'], $save_dir . '/' . $rename_to); return tb_post_thumb_gen_image($save_dir . '/' . $rename_to, $imagelocation, $alt_text, $generate); } // sticky bit? if (!is_dir($save_dir)) { mkdir($save_dir, 0777); } // manipulate thumbnails $thumb = new ImageEditor($dest_path['basename'], $dest_path['dirname'] . '/'); $thumb->resize($settings['resize_width'], $settings['resize_height'], $settings['keep_ratio']); if ($settings['crop_exact'] == 'true' || $crop_x != 0 && $crop_y != 0) { if ($crop_x != 0 && $crop_y != 0) { $settings['resize_width'] = $crop_x; $settings['resize_height'] = $crop_y; } if ($thumb->x > $settings['resize_width'] || $thumb->y > $settings['resize_height']) { $thumb->crop((int) (($thumb->x - $settings['resize_width']) / 2), 0, $settings['resize_width'], $settings['resize_height']); } } $thumb->outputFile($save_dir . "/" . $rename_to, ""); chmod($save_dir . "/" . $rename_to, 0666); $imagelocation = str_replace($settings['base_path'], $settings['full_domain_name'], $save_dir . '/' . $rename_to); return tb_post_thumb_gen_image($save_dir . '/' . $rename_to, $imagelocation, $alt_text, $generate); } else { if (!empty($settings['video_regex']) && tb_post_thumb_check_video($settings['video_regex'])) { $settings['default_image'] = $settings['video_default']; } return tb_post_thumb_gen_image(str_replace($settings['full_domain_name'], $settings['base_path'], $settings['default_image']), $settings['default_image'], $alt_text, $generate); } }
if ($imSize[0] * $imSize[1] > $MaxSize * 1024000) { error_gif('Фото слишком большое. Максимальный размер - ' . $MaxSize . ' Мегапикселей.'); } if ($flag_ok == 0) { error_gif('Извините, превью не сгенерировано администратором.'); } $editor = new ImageEditor($file_local); if (!$editor->ready) { error_gif('Не удалось открыть файл.'); } if (!$w) { $editor->resizeToHeight($h); } elseif (!$h) { $editor->resizeToWidth($w); } else { $editor->resize($w, $h, $fitInside); } ob_start(); // Чтобы далее узнать точный размер файла $editor->output(); header_img(2, ob_get_length(), time(), substr($file_name, 0, 20) . '.jpg'); ob_end_flush(); // Кэшируем if ($LocalCache) { $editor->save($LocalCache . $file_name, IMAGETYPE_JPEG, $q); } exit; // Показываем картинку с текстом ошибки function error_gif($t) { $vsize = 46;