Пример #1
0
 function load_foto($file, $img_old, $img_small_old, $x, $y)
 {
     global $user;
     require_once 'classes/thumblib.inc.php';
     require_once 'classes/resize.class.php';
     require_once 'classes/watermark.class.php';
     $size = new Resize();
     $folder = 'files/foto/' . $user['id'] . '/';
     @mkdir($folder, 0777);
     $size->dir = $folder;
     // Директория изображений
     $size->original = true;
     if ($file['name']) {
         $size->setResize($file);
         if (!$size->error) {
             @unlink($img_old);
             @unlink($img_small_old);
             $img_small = $size->small;
             $img = $size->image;
             $thumb = PhpThumbFactory::create($img);
             $thumb->adaptiveresize($x, $y);
             $thumb->save($img_small);
             $img_small = $img_small;
             $img_small2 = str_replace("_s", "_m", $size->small);
             $thumb = PhpThumbFactory::create($img);
             $thumb->adaptiveresize(150, 100);
             $thumb->save($img_small2);
             $thumb = PhpThumbFactory::create($img);
             $thumb->Resize(637, 0);
             $thumb->save($img);
             $path_info = pathinfo($img_small);
             $img_small = str_replace($path_info['extension'], 'jpg', $img_small);
             $this->convert_foto($size->small, $img_small);
             //@unlink($size->small);
             $path_info = pathinfo($img);
             $img = str_replace($path_info['extension'], 'jpg', $size->image);
             $this->convert_foto($size->image, $img);
             //@unlink($size->image);
             $watermark = new watermark();
             # создаем объекты-изображения используя исходные файлы (main.jpg и watermark.png)
             $main_img_obj = imagecreatefromjpeg($img);
             $watermark_img_obj = imagecreatefrompng('watermark.png');
             # создаем изображение с водяным знаком - значение прозрачности альфа-канала водяного знака установим в 66%
             $return_img_obj = $watermark->create_watermark($main_img_obj, $watermark_img_obj, 66, $img, $ext);
             # отобразим наше полученное изображение в браузере - но сначала сообщим ему, что это jpeg-файл
             $img = $img;
         } else {
             $error = $size->error;
         }
     } else {
         $img_small = $img_small_old;
         $img = $img_old;
     }
     $result = array('error' => $error, 'img' => $img, 'img_small' => $img_small);
     return $result;
 }
Пример #2
0
 /**
  * Process the actions, crop, scale(resize), rotate, flip, and save.
  * When ever an action is performed, the result is save into a
  * temporary image file, see createUnique on the filename specs.
  * It does not return the saved file, alway returning the tmp file.
  * @param string $action, should be 'crop', 'scale', 'rotate','flip', or 'save'
  * @param string $relative the relative image filename
  * @param string $fullpath the fullpath to the image file
  * @return array with image information
  * <code>array('src'=>'url of the image', 'dimensions'=>'width="xx" height="yy"',
  * 'file'=>'image file, relative', 'fullpath'=>'full path to the image');</code>
  */
 function processAction($action, $relative, $fullpath)
 {
     $params = '';
     if (isset($_GET['params'])) {
         $params = $_GET['params'];
     }
     $values = explode(',', $params, 4);
     $saveFile = $this->getSaveFileName($values[0]);
     $img = Image_Transform::factory(IMAGE_CLASS);
     $img->load($fullpath);
     switch ($action) {
         case 'replace':
             // 'ImageManager.php' handled the uploaded file, it's now on the server.
             // If maximum size is specified, constrain image to it.
             $dimensionsIndex = isset($_REQUEST['uploadSize']) ? $_REQUEST['uploadSize'] : 0;
             if ($this->manager->config['maxWidth'][$dimensionsIndex] > 0 && $this->manager->config['maxHeight'][$dimensionsIndex] > 0 && ($img->img_x > $this->manager->config['maxWidth'][$dimensionsIndex] || $img->img_y > $this->manager->config['maxHeight'][$dimensionsIndex])) {
                 $percentage = min($this->manager->config['maxWidth'][$dimensionsIndex] / $img->img_x, $this->manager->config['maxHeight'][$dimensionsIndex] / $img->img_y);
                 $img->scale($percentage);
             }
             break;
         case 'watermark':
             // loading target image
             $functionName = 'ImageCreateFrom' . $img->type;
             if (function_exists($functionName)) {
                 $imageResource = $functionName($fullpath);
             } else {
                 echo "<script>alert(\"Error when loading '" . basename($fullpath) . "' - Loading '" . $img->type . "' files not supported\");</script>";
                 return false;
             }
             // loading watermark
             $watermarkFullPath = $_GET['watermarkFullPath'];
             $watermarkImageType = strtolower(substr($watermarkFullPath, strrpos($watermarkFullPath, ".") + 1));
             if ($watermarkImageType == "jpg") {
                 $watermarkImageType = "jpeg";
             }
             if ($watermarkImageType == "tif") {
                 $watermarkImageType = "tiff";
             }
             $functionName = 'ImageCreateFrom' . $watermarkImageType;
             if (function_exists($functionName)) {
                 $watermarkResource = $functionName($watermarkFullPath);
             } else {
                 echo "<script>alert(\"Error when loading '" . basename($watermarkFullPath) . "' - Loading '" . $img->type . "' files not supported\");</script>";
                 return false;
             }
             $numberOfColors = imagecolorstotal($watermarkResource);
             $watermarkX = isset($_GET['watermarkX']) ? $_GET['watermarkX'] : -1;
             $watermarkY = isset($_GET['watermarkY']) ? $_GET['watermarkY'] : -1;
             $opacity = $_GET['opacity'];
             // PNG24 watermark on GIF target needs special handling
             // PNG24 watermark with alpha transparency on other targets need also this handling
             if ($watermarkImageType == "png" && $numberOfColors == 0 && ($img->type == "gif" || $opacity < 100)) {
                 require_once 'Classes/api.watermark.php';
                 $watermarkAPI = new watermark();
                 $imageResource = $watermarkAPI->create_watermark($imageResource, $watermarkResource, $opacity, $watermarkX, $watermarkY);
             } elseif ($watermarkImageType == "png" && $numberOfColors == 0 && $opacity == 100) {
                 $watermark_width = imagesx($watermarkResource);
                 $watermark_height = imagesy($watermarkResource);
                 imagecopy($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height);
             } else {
                 $watermark_width = imagesx($watermarkResource);
                 $watermark_height = imagesy($watermarkResource);
                 imagecopymerge($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height, $opacity);
             }
             break;
         case 'crop':
             $img->crop(intval($values[0]), intval($values[1]), intval($values[2]), intval($values[3]));
             break;
         case 'scale':
             $img->resize(intval($values[0]), intval($values[1]));
             break;
         case 'rotate':
             $img->rotate(floatval($values[0]));
             break;
         case 'flip':
             if ($values[0] == 'hoz') {
                 $img->flip(true);
             } else {
                 if ($values[0] == 'ver') {
                     $img->flip(false);
                 }
             }
             break;
         case 'save':
             if (!is_null($saveFile)) {
                 $quality = intval($values[1]);
                 if ($quality < 0) {
                     $quality = 85;
                 }
                 $newSaveFile = $this->makeRelative($relative, $saveFile);
                 $oldSaveFile = $newSaveFile;
                 if ($this->manager->config['allow_newFileName'] && !$this->manager->config['allow_overwrite']) {
                     // check whether a file already exist and if there is, create a variant of the filename
                     $newName = $this->getUniqueFilename($newSaveFile);
                     //get unique filename just returns the filename, so
                     //we need to make the relative path again.
                     $newSaveFile = $this->makeRelative($relative, $newName);
                 }
                 // forced new name?
                 if ($oldSaveFile != $newSaveFile) {
                     $this->forcedNewName = $newName;
                 } else {
                     $this->forcedNewName = false;
                 }
                 $newSaveFullpath = $this->manager->getFullPath($newSaveFile);
                 $img->save($newSaveFullpath, $values[0], $quality);
                 if (is_file($newSaveFullpath)) {
                     $this->filesaved = 1;
                 } else {
                     $this->filesaved = -1;
                 }
             }
             break;
     }
     //create the tmp image file
     $filename = $this->createUnique($fullpath);
     $newRelative = $this->makeRelative($relative, $filename);
     $newFullpath = $this->manager->getFullPath($newRelative);
     $newURL = $this->manager->getFileURL($newRelative);
     // when uploaded and not resized, rename and don't save
     if ($action == "replace" && $percentage <= 0) {
         rename($fullpath, $newFullpath);
     } elseif ($action == "watermark") {
         // save image
         $functionName = 'image' . $img->type;
         if (function_exists($functionName)) {
             if ($type == 'jpeg') {
                 $functionName($imageResource, $newFullpath, 100);
             } else {
                 $functionName($imageResource, $newFullpath);
             }
         } else {
             echo "<script>alert(\"Error when saving '" . basename($newFullpath) . "' - Saving '" . $img->type . "' files not supported\");</script>";
             return false;
         }
     } else {
         //save the file.
         $img->save($newFullpath);
         $img->free();
     }
     // when uploaded was resized and saved, remove original
     if ($action == "replace" && $percentage > 0) {
         unlink($fullpath);
     }
     //get the image information
     $imgInfo = @getimagesize($newFullpath);
     $image['src'] = $newURL;
     $image['dimensions'] = $imgInfo[3];
     $image['width'] = $imgInfo[0];
     $image['height'] = $imgInfo[1];
     $image['file'] = $newRelative;
     $image['fullpath'] = $newFullpath;
     return $image;
 }
function add_watermark($source_img, $watermark_img, $destination_folder, $filename)
{
    $watermark = new watermark();
    if ($source_img && $watermark_img) {
        $main_img_obj = imagecreatefromjpeg($source_img);
        $watermark_img_obj = imagecreatefrompng($watermark_img);
        # create our watermarked image
        $return_img_obj = $watermark->create_watermark($main_img_obj, $watermark_img_obj, $alpha_level = 100, $watermark_v_position = WATERMARK_V_POSITION, $watermark_h_position = WATERMARK_H_POSITION);
        # create watermarked image
        imagejpeg($return_img_obj, $destination_folder . $filename, 80);
    }
}