mkdir($mediadir, 0777, true); } $filename = $query_string['id'] . '.' . $query_string['hash'] . '.' . $query_string['ext']; $writeTo = $mediadir . $filename; if (!$width || !$height) { header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); exit; } if (!($size = getimagesize($fullPath))) { header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); exit; } list($currentWidth, $currentHeight, $currentType) = $size; if (class_exists('imagick')) { $new_image_obj = new imagick($fullPath); $new_image = $new_image_obj->clone(); $new_image->setImageColorspace(Imagick::COLORSPACE_RGB); $new_image->flattenImages(); if ($is_beyond_original && ($width > $currentWidth || $height > $currentHeight)) { $width = $currentWidth; $height = $currentHeight; } if (!$aspect) { $new_image->cropThumbnailImage($width, $height); } else { $new_image->scaleImage($width, $height, false); } $new_image->writeImage($writeTo); } else { $target['width'] = $currentWidth; $target['height'] = $currentHeight;
// узнаем размеры водяного знака $watermark_width = $watermark->getImageWidth(); $watermark_height = $watermark->getImageHeight(); // посчитать x и y $left = $im_width - $watermark_width - 10; $top = $im_height - $watermark_height - 10; // накладываем watermark на оригинальное изображение $im->compositeImage($watermark, imagick::COMPOSITE_OVER, $left, $top); // сохраняем оригинал $im->writeImage('./images/' . $image_name . '.' . $p[1]); } else { // сохраняем .gif с учетом анимации $im->writeImages('./images/' . $image_name . '.' . $p[1], true); } // Копируем объект для различных типов $large = $im->clone(); $square = $im->clone(); // Создаем квадратное изображение 160x160 px $square->cropThumbnailImage(160, 160); $square->writeImage('./images/' . $image_name . 's' . '.' . $p[1]); // Создаем большое изображение с шириной 640 px и переменной высотой $large->thumbnailImage(640, 0); $large->writeImage('./images/' . $image_name . 'l' . '.' . $p[1]); // добавляем имя, расширение, дату загрузки изображения в БД // выбираем коллекцию $collection = $db->images; // добавляем новый документ - изображение в коллекцию Images $collection->insert(array('image' => $image_name, 'ext' => $p[1], 'title' => '', 'views' => 0, 'gallery' => 0, 'date' => date("d-m-Y H:i:s"))); //$image_names[] = $image_name; $_SESSION['image_name'] = $image_name; echo $image_name;
function resizeFile($fullPath, $width = 600, $height = 400, $writeTo, $aspect = true, $is_beyond_original = false, $query_string = array()) { if (!$width || !$height) { return false; } if (!($size = getimagesize($fullPath))) { return false; } list($currentWidth, $currentHeight, $currentType) = $size; $return = false; if (class_exists('imagick')) { $new_image_obj = new imagick($fullPath); $new_image = $new_image_obj->clone(); $new_image->setImageColorspace(Imagick::COLORSPACE_RGB); $new_image->flattenImages(); if ($is_beyond_original && ($width > $currentWidth || $height > $currentHeight)) { $width = $currentWidth; $height = $currentHeight; } if (!$aspect) { $new_image->cropThumbnailImage($width, $height); } else { $new_image->scaleImage($width, $height, false); } if ($new_image->writeImage($writeTo)) { $return = true; } } else { $target['width'] = $currentWidth; $target['height'] = $currentHeight; $target['x'] = $target['y'] = 0; $types = array(1 => 'gif', 'jpeg', 'png', 'swf', 'psd', 'wbmp'); _setMemoryLimitForImage($fullPath); $image = call_user_func('imagecreatefrom' . $types[$currentType], $fullPath); ini_restore('memory_limit'); if ($aspect) { if ($currentHeight / $height > $currentWidth / $width) { $width = ceil($currentWidth / $currentHeight * $height); } else { $height = ceil($width / ($currentWidth / $currentHeight)); } } else { $proportion_X = $currentWidth / $width; $proportion_Y = $currentHeight / $height; if ($proportion_X > $proportion_Y) { $proportion = $proportion_Y; } else { $proportion = $proportion_X; } $target['width'] = $width * $proportion; $target['height'] = $height * $proportion; $original['diagonal_center'] = round(sqrt($currentWidth * $currentWidth + $currentHeight * $currentHeight) / 2); $target['diagonal_center'] = round(sqrt($target['width'] * $target['width'] + $target['height'] * $target['height']) / 2); $crop = round($original['diagonal_center'] - $target['diagonal_center']); if ($proportion_X < $proportion_Y) { $target['x'] = 0; $target['y'] = round($currentHeight / 2 * $crop / $target['diagonal_center']); } else { $target['x'] = round($currentWidth / 2 * $crop / $target['diagonal_center']); $target['y'] = 0; } } if ($is_beyond_original && ($width > $currentWidth || $height > $currentHeight)) { $width = $currentWidth; $height = $currentHeight; } if (function_exists('imagecreatetruecolor') && ($temp = imagecreatetruecolor($width, $height))) { imagecopyresampled($temp, $image, 0, 0, $target['x'], $target['y'], $width, $height, $target['width'], $target['height']); } else { $temp = imagecreate($width, $height); imagecopyresized($temp, $image, 0, 0, 0, 0, $width, $height, $currentWidth, $currentHeight); } if (strtolower($query_string['ext']) == 'png') { imagepng($temp, $writeTo); } else { if (strtolower($query_string['ext']) == 'jpg' || strtolower($query_string['ext']) == 'jpeg') { imagejpeg($temp, $writeTo, 100); } else { if (strtolower($query_string['ext']) == 'gif') { imagegif($temp, $writeTo); } } } ob_start(); call_user_func('image' . $types[$currentType], $temp); $return = ob_get_clean(); imagedestroy($image); imagedestroy($temp); } return $return; }