Ejemplo n.º 1
0
<?php

ignore_user_abort(true);
set_time_limit(0);
// pixel cache max size
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
$mid = $_REQUEST['mid'];
mkdir("../upload/");
$fname = md5($file_name[1] . date() . rand(0, 100000)) . ".jpg";
while (file_exists("../../magazine/" . $mid . "/" . $fname)) {
    $fname = md5($file_name[1] . date() . rand(0, 100000)) . ".jpg";
}
move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $fname);
$img = new imagick();
$img->setResolution(500, 500);
$img->readimage("../upload/" . $fname);
$img->scaleImage(1000, 0);
$img->writeImage("../../magazine/" . $mid . "/" . $fname);
$img->scaleImage(200, 0);
$img->writeImage("../../magazine/" . $mid . "/small/" . $fname);
include "conn.php";
$sql = "SELECT `magazine`.`size` FROM `magazine` WHERE `magazine`.`id`=" . $mid . "";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);
$sql = "INSERT INTO `pages` (`magazine`, `name`, `position`) VALUES (" . $mid . ", '" . $fname . "', '" . $row[0] . "');";
mysql_query($sql, $conn);
$sql = "UPDATE `magazine` SET size=size+1 WHERE `magazine`.`id`='" . $mid . "'";
mysql_query($sql, $conn);
echo mysql_insert_id($conn);
Ejemplo n.º 2
0
 public function resize($maxwidth = 0, $maxheight = 0, $opts = array())
 {
     $config = self::getConfig();
     if ($config['cache'] && ($cache = $this->getCache())) {
         $this->_data = $cache;
         return $this->_data;
     }
     if (is_array($maxwidth)) {
         $opts = $maxwidth;
     } else {
         $opts['width'] = $maxwidth;
         $opts['height'] = $maxheight;
     }
     $origWidth = $width = $this->_imagesize[0];
     $origHeight = $height = $this->_imagesize[1];
     if (isset($opts['color'])) {
         $color = preg_replace('/[^0-9a-fA-F]/', '', (string) $opts['color']);
     } else {
         $color = FALSE;
     }
     if ((!isset($opts['width']) || !$opts['width']) && isset($opts['height'])) {
         $opts['width'] = 99999999999999.0;
     } elseif (isset($opts['width']) && (!isset($opts['height']) || !$opts['height'])) {
         $opts['height'] = 99999999999999.0;
     }
     if (!isset($opts['width'])) {
         $opts['width'] = $width;
     }
     if (!isset($opts['height'])) {
         $opts['height'] = $height;
     }
     $offsetX = 0;
     $offsetY = 0;
     if (isset($opts['ratio']) && $opts['ratio']) {
         //$cropRatio = explode(':', (string) $opts['ratio']);
         $cropRatio = array($opts['width'], $opts['height']);
         if (sizeof($cropRatio) == 2) {
             $ratioComputed = $width / $height;
             $cropRatioComputed = (double) $cropRatio[0] / (double) $cropRatio[1];
             if ($ratioComputed < $cropRatioComputed) {
                 $origHeight = $height;
                 $height = $width / $cropRatioComputed;
                 $offsetY = ($origHeight - $height) / 2;
             } else {
                 if ($ratioComputed > $cropRatioComputed) {
                     $origWidth = $width;
                     $width = $height * $cropRatioComputed;
                     $offsetX = ($origWidth - $width) / 2;
                 }
             }
         }
     }
     if (isset($opts["x"])) {
         $offsetX = (int) $opts["x"];
     }
     if (isset($opts["y"])) {
         $offsetY = (int) $opts["y"];
     }
     $xRatio = $opts['width'] / $width;
     $yRatio = $opts['height'] / $height;
     if ($xRatio * $height < $opts['height']) {
         $tnHeight = ceil($xRatio * $height);
         $tnWidth = $opts['width'];
     } else {
         $tnWidth = ceil($yRatio * $width);
         $tnHeight = $opts['height'];
     }
     // Determine the quality of the output image
     $quality = isset($opts['quality']) ? (int) $opts['quality'] : $config['quality'];
     $resizeFactor = isset($opts['scale']) && (double) $opts['scale'] != 0 ? (double) $opts['scale'] : 1;
     if (isset($opts['crop']) && sizeof($cropRatio) == 2) {
         $dst = imagecreatetruecolor($cropRatio[0] * $resizeFactor, $cropRatio[1] * $resizeFactor);
         //$dst = imagecreatetruecolor($cropRatio[0], $cropRatio[1]);
     } else {
         $dst = imagecreatetruecolor($tnWidth * $resizeFactor, $tnHeight * $resizeFactor);
         //$dst = imagecreatetruecolor($tnWidth, $tnHeight*$resizeFactor);
     }
     switch ($this->_mime) {
         case 'image/gif':
             $creationFunction = 'imagecreatefromgif';
             $outputFunction = 'imagepng';
             $mime = 'image/png';
             // We need to convert GIFs to PNGs
             $doSharpen = FALSE;
             $quality = round(10 - $quality / 10);
             break;
         case 'image/x-png':
         case 'image/png':
             $creationFunction = 'imagecreatefrompng';
             $outputFunction = 'imagepng';
             $doSharpen = FALSE;
             $quality = round(10 - $quality / 10);
             // PNG needs a compression level of 0 (no compression) through 9
             break;
         default:
             $creationFunction = 'imagecreatefromjpeg';
             $outputFunction = 'imagejpeg';
             $doSharpen = TRUE;
             break;
     }
     // Read in the original image
     $src = $creationFunction($this->_filename);
     if (in_array($this->_mime, array('image/gif', 'image/png'))) {
         if (!$color) {
             // If this is a GIF or a PNG, we need to set up transparency
             imagealphablending($dst, false);
             imagesavealpha($dst, true);
         } else {
             // Fill the background with the specified color for matting purposes
             if ($color[0] == '#') {
                 $color = substr($color, 1);
             }
             $background = FALSE;
             if (strlen($color) == 6) {
                 $background = imagecolorallocate($dst, hexdec($color[0] . $color[1]), hexdec($color[2] . $color[3]), hexdec($color[4] . $color[5]));
             } else {
                 if (strlen($color) == 3) {
                     $background = imagecolorallocate($dst, hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] . $color[2]));
                 }
             }
             if ($background) {
                 imagefill($dst, 0, 0, $background);
             }
         }
     }
     if (isset($opts['crop']) && !empty($opts['crop'])) {
         if ($width > $height) {
             $destW = $tnWidth / $tnHeight * $height * ($cropRatio[0] / $tnWidth);
             $destH = $height * ($cropRatio[0] / $tnWidth);
         } else {
             $destH = $tnHeight / $tnWidth * $width * ($cropRatio[0] / $tnWidth);
             $destW = $width * ($cropRatio[0] / $tnWidth);
         }
     } else {
         if (isset($opts['crop_width']) && isset($opts['crop_height'])) {
             $destW = (int) $opts['crop_width'];
             $destH = (int) $opts['crop_height'];
             $width = $origWidth;
             $height = $origHeight;
             $offsetX = $offsetX * ($width / $destW);
             $offsetY = $offsetY * ($height / $destH);
         } else {
             $destH = $tnHeight;
             $destW = $tnWidth;
         }
     }
     $w = $width;
     $h = $height;
     $dw = $destW * $resizeFactor;
     //$dw = $destW;
     $dh = $destH * $resizeFactor;
     //$dh = $destH;
     // Resample the original image into the resized canvas we set up earlier
     imagecopyresampled($dst, $src, 0, 0, $offsetX, $offsetY, $dw, $dh, $w, $h);
     if ($doSharpen) {
         $sharpness = self::findSharp($width, $tnWidth);
         $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1));
         $divisor = $sharpness;
         $offset = 0;
         imageconvolution($dst, $sharpenMatrix, $divisor, $offset);
     }
     if (isset($opts['blackwhite']) && $opts['blackwhite']) {
         for ($c = 0; $c < 256; $c++) {
             $palette[$c] = imagecolorallocate($dst, $c, $c, $c);
         }
         //Creates yiq function
         function yiq($r, $g, $b)
         {
             return $r * 0.299 + $g * 0.587 + $b * 0.114;
         }
         for ($y = 0; $y < $destH; $y++) {
             for ($x = 0; $x < $destW; $x++) {
                 $rgb = imagecolorat($dst, $x, $y);
                 $r = $rgb >> 16 & 0xff;
                 $g = $rgb >> 8 & 0xff;
                 $b = $rgb & 0xff;
                 //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
                 $gs = yiq($r, $g, $b);
                 imagesetpixel($dst, $x, $y, $palette[$gs]);
             }
         }
     }
     if (isset($opts['rotation']) && !empty($opts['rotation'])) {
         $dst = imagerotate($dst, (double) $opts['rotation'], 0);
     }
     try {
         /*
         
         		Filters :
         
         			IMG_FILTER_GRAYSCALE: Converts the image into grayscale.
         			IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Use arg1 to set the level of brightness.
         			IMG_FILTER_CONTRAST: Changes the contrast of the image. Use arg1 to set the level of contrast.
         			IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use arg1, arg2 and arg3 in the form of red, blue, green and arg4 for the alpha channel. The range for each color is 0 to 255.
         			IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
         			IMG_FILTER_EMBOSS: Embosses the image.
         			IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.
         			IMG_FILTER_SELECTIVE_BLUR: Blurs the image.
         			IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a "sketchy" effect.
         			IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness.
         			IMG_FILTER_PIXELATE: Applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode.
         */
         if (isset($opts['IMG_FILTER_NEGATE']) && !empty($opts['IMG_FILTER_NEGATE'])) {
             imagefilter($dst, IMG_FILTER_NEGATE);
         }
         if (isset($opts['IMG_FILTER_GRAYSCALE']) && !empty($opts['IMG_FILTER_GRAYSCALE'])) {
             imagefilter($dst, IMG_FILTER_GRAYSCALE);
         }
         if (isset($opts['IMG_FILTER_BRIGHTNESS']) && !empty($opts['IMG_FILTER_BRIGHTNESS'])) {
             imagefilter($dst, IMG_FILTER_BRIGHTNESS, intval($opts['IMG_FILTER_BRIGHTNESS']));
         }
         if (isset($opts['IMG_FILTER_CONTRAST']) && !empty($opts['IMG_FILTER_CONTRAST'])) {
             imagefilter($dst, IMG_FILTER_CONTRAST, intval($opts['IMG_FILTER_CONTRAST']));
         }
         if (isset($opts['IMG_FILTER_COLORIZE']) && !empty($opts['IMG_FILTER_COLORIZE'])) {
             if (strlen($opts['IMG_FILTER_COLORIZE']) == 12) {
                 $arg1 = intval(substr($opts['IMG_FILTER_COLORIZE'], 0, 3));
                 $arg2 = intval(substr($opts['IMG_FILTER_COLORIZE'], 3, 3));
                 $arg3 = intval(substr($opts['IMG_FILTER_COLORIZE'], 6, 3));
                 $arg4 = intval(substr($opts['IMG_FILTER_COLORIZE'], 9, 3));
                 imagefilter($dst, IMG_FILTER_COLORIZE, $arg1, $arg2, $arg3, $arg4);
             }
         }
         if (isset($opts['IMG_FILTER_EDGEDETECT']) && !empty($opts['IMG_FILTER_EDGEDETECT'])) {
             imagefilter($dst, IMG_FILTER_EDGEDETECT);
         }
         if (isset($opts['IMG_FILTER_EMBOSS']) && !empty($opts['IMG_FILTER_EMBOSS'])) {
             imagefilter($dst, IMG_FILTER_EMBOSS);
         }
         if (isset($opts['IMG_FILTER_GAUSSIAN_BLUR']) && !empty($opts['IMG_FILTER_GAUSSIAN_BLUR'])) {
             imagefilter($dst, IMG_FILTER_GAUSSIAN_BLUR);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_SELECTIVE_BLUR']) && !empty($opts['IMG_FILTER_SELECTIVE_BLUR'])) {
             imagefilter($dst, IMG_FILTER_SELECTIVE_BLUR);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_MEAN_REMOVAL']) && !empty($opts['IMG_FILTER_MEAN_REMOVAL'])) {
             imagefilter($dst, IMG_FILTER_MEAN_REMOVAL);
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_SMOOTH']) && !empty($opts['IMG_FILTER_SMOOTH'])) {
             imagefilter($dst, IMG_FILTER_SMOOTH, intval($opts['IMG_FILTER_SMOOTH']));
         }
         //: Reverses all colors of the image.
         if (isset($opts['IMG_FILTER_PIXELATE']) && !empty($opts['IMG_FILTER_PIXELATE'])) {
             if (strlen($opts['IMG_FILTER_PIXELATE']) > 3) {
                 $arg1 = intval(substr($opts['IMG_FILTER_PIXELATE'], 0, 3));
                 $arg2 = $opts['IMG_FILTER_PIXELATE'] == true;
             } else {
                 $arg1 = intval($opts['IMG_FILTER_PIXELATE']);
                 $arg2 = false;
             }
             imagefilter($dst, IMG_FILTER_PIXELATE, $arg1, $arg2);
         }
         //: Reverses all colors of the image.
     } catch (Exception $e) {
         var_dump($e);
     }
     ob_start();
     $outputFunction($dst, null, $quality);
     $this->_data = ob_get_contents();
     ob_end_clean();
     imagedestroy($src);
     imagedestroy($dst);
     //The following depends on Imagick
     //$this->_image is the Imagick Object; add calls to the different native functions as needed, it is returned as $this->_data at the end.
     if (class_exists('Imagick') && (isset($opts['cropx']) || isset($opts['cropy']) || isset($opts['brightness']) || isset($opts['saturation']) || isset($opts['hue']) || isset($opts['contrast']) || isset($opts['composite']))) {
         IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 512);
         $this->_image = new Imagick();
         $this->_image->readImageBlob($this->_data);
         //For image cropping
         if (isset($opts['cropx']) && !empty($opts['cropx'])) {
             if (!isset($opts['cropy']) || empty($opts['cropy'])) {
                 $opts['cropy'] = 0;
             }
             if (isset($opts['lencropx']) && !empty($opts['lencropx'])) {
                 $maxwidth = $opts['lencropx'];
             } else {
                 $maxwidth = $config['maxLengthCropX'];
             }
             if (isset($opts['lencropy']) && !empty($opts['lencropy'])) {
                 $maxheight = $opts['lencropy'];
             } else {
                 $maxheight = $config['maxLengthCropY'];
             }
             $this->_image->cropImage($maxwidth, $maxheight, $opts['cropx'], $opts['cropy']);
         } else {
             if (isset($opts['cropy']) && !empty($opts['cropy'])) {
                 if (!isset($opts['cropx']) || empty($opts['cropx'])) {
                     $opts['cropx'] = 0;
                 }
                 if (isset($opts['lencropx']) && !empty($opts['lencropx'])) {
                     $maxwidth = $opts['lencropx'];
                 } else {
                     $maxwidth = 410;
                 }
                 if (isset($opts['lencropy']) && !empty($opts['lencropy'])) {
                     $maxheight = $opts['lencropy'];
                 } else {
                     $maxheight = 275;
                 }
                 $this->_image->cropImage($maxwidth, $maxheight, $opts['cropx'], $opts['cropy']);
             }
         }
         //Modulate: 100 is the norm
         if (isset($opts['brightness']) && !empty($opts['brightness'])) {
             $this->_image->modulateImage($opts['brightness'], 100, 100);
         }
         if (isset($opts['saturation']) && !empty($opts['saturation'])) {
             $this->_image->modulateImage(100, $opts['saturation'], 100);
         }
         if (isset($opts['hue']) && !empty($opts['hue'])) {
             $this->_image->modulateImage(100, 100, $opts['hue']);
         }
         if (isset($opts['contrast']) && !empty($opts['contrast'])) {
             $this->_image->contrastImage($opts['contrast']);
         }
         //To blend 2 images
         if (isset($opts['composite']) && !empty($opts['composite'])) {
             $over = new Imagick($opts['composite']);
             $d = $this->_image->getImageGeometry();
             $w = $d['width'];
             $h = $d['height'];
             $over->thumbnailImage($w, $h);
             $this->_image->compositeImage($over, imagick::COMPOSITE_DEFAULT, 0, 0);
             $over->clear();
             $over->destroy();
         }
         $this->_data = $this->_image->getImageBlob();
         $this->_image->clear();
         $this->_image->destroy();
     }
     if ($config['cache']) {
         $this->saveCache($opts);
     }
     return $this->_data;
 }
Ejemplo n.º 3
0
 public function editAction()
 {
     $types = $this->config->fa->upload_types->toArray();
     $id = (int) $this->getParam('id');
     if ($id !== 0) {
         // Edit existing record.
         // TODO: Reimplement administrator access.
         $record = Upload::getRepository()->findOneBy(array('id' => $id, 'user_id' => $this->user->id));
         if (!$record instanceof Upload) {
             throw new \FA\Exception('Submission ID not found!');
         }
         $edit_mode = true;
         $type = $record->submission_type;
     } else {
         // Create new submission.
         $type = $this->getParam('type');
         // Show type selector if no type is specified.
         if (empty($type) || !isset($types[$type])) {
             $this->view->types = $types;
             return $this->view->pick('uploads/select');
         }
         $edit_mode = false;
         $record = NULL;
     }
     $type_info = $types[$type];
     $form_config = $this->current_module_config->forms->uploads_edit->toArray();
     // Create mode changes
     if (!$edit_mode) {
         $form_config['groups']['files']['elements']['submission'][1]['required'] = true;
         unset($form_config['groups']['files']['elements']['submission'][1]['description']);
         unset($form_config['groups']['files']['elements']['rebuild_thumbnail']);
     }
     // Changes to the form based on submission type.
     if (isset($type_info['category'])) {
         $form_config['groups']['metadata']['elements']['category'][1]['default'] = $type_info['category'];
     }
     if ($type !== Upload::TYPE_IMAGE) {
         unset($form_config['groups']['files']['elements']['rebuild_thumbnail']);
     }
     $form_config['groups']['files']['elements']['submission'][1]['allowedTypes'] = $type_info['types'];
     // Create the form class.
     $form = new \FA\Form($form_config);
     // Populate the form (if available).
     if ($record instanceof Upload) {
         $form->setDefaults($record->toArray(TRUE, TRUE));
     }
     // Handle form submission.
     if ($_POST && $this->request->hasFiles() && $form->isValid(array_merge($_POST, $_FILES))) {
         $data = $form->getValues();
         if (!$record instanceof Upload) {
             $record = new Upload();
             $record->upload_type = $type;
             $record->user = $this->user;
             $record->is_hidden = true;
             // Hide until properly populated.
             $record->save();
             // Immediately save to generate IDs used in next steps.
         }
         $record->fromArray($data);
         // Begin file handling.
         \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MEMORY, 32);
         \IMagick::setResourceLimit(\Imagick::RESOURCETYPE_MAP, 32);
         $files = $form->getFiles($this->request);
         $imagine = new Imagine();
         $submission_file = $files['submission'][0];
         $thumbnail_file = null;
         $thumbnail_paths = array();
         $preview_file = null;
         $preview_paths = array();
         if ($submission_file) {
             $submission_paths = $record->generatePaths($submission_file->getName());
             // Create the proper artwork directory if it doesn't exist.
             $submission_dir = dirname($submission_paths['full']['path']);
             @mkdir($submission_dir);
             if ($type == Upload::TYPE_IMAGE) {
                 // Handle image uploads.
                 $submission_image = $imagine->open($submission_file->getTempName());
                 $is_animated = count($submission_image->layers()) > 1;
                 // So, it seems Imagine really loves to screw up GIFs, so lets avoid that
                 if ($is_animated) {
                     $dest_path = $submission_paths['full']['path'];
                     // Copying this instead of moving due to the file being reused by preview/thumbnail
                     copy($submission_file->getTempName(), $dest_path);
                 } else {
                     $submission_image->save($submission_paths['full']['path'], array('animated' => $is_animated));
                 }
                 // Make this file the thumbnail if no other is specified.
                 if (empty($files['thumbnail']) && (!$edit_mode || $data['rebuild_thumbnail'])) {
                     $thumbnail_file = $submission_file;
                     $thumbnail_paths = $submission_paths;
                 }
                 // Set up the preview parameters
                 $preview_file = $submission_file;
                 $preview_paths = $submission_paths;
             } else {
                 // Handle non-images. Way simpler, right?
                 $dest_path = $submission_paths['full']['path'];
                 $submission_file->moveTo($dest_path);
                 // Prevent the file from being deleted below.
                 $submission_file = null;
             }
             $record->setFull($submission_paths['full']['base']);
         }
         // Use the thumbnail field if supplied.
         if (!empty($files['thumbnail'])) {
             $thumbnail_file = $files['thumbnail'][0];
             $thumbnail_paths = $record->generatePaths($thumbnail_file->getName());
         }
         // If we haven't set a preview image/path, then use the thumbnail if possible
         if (is_null($preview_file)) {
             $preview_file = $thumbnail_file;
             $preview_paths = $thumbnail_paths;
         }
         // Process either the uploaded thumbnail, or resize the original file to be our preview.
         if ($preview_file) {
             // Generate "small" size thumbnail.
             $preview_size = new Box(self::MAX_PREVIEW_SIZE, self::MAX_PREVIEW_SIZE);
             self::_saveAsJPG($imagine, $preview_file, $preview_paths['small']['path'], 90, $preview_size);
             $record->setSmall(self::_changeExtension($preview_paths['small']['base'], 'jpg'));
         }
         // Process either the uploaded thumbnail, or thumbnailize the original file.
         if ($thumbnail_file) {
             // Generate "thumb" size thumbnail.
             $thumbnail_size = new Box(self::MAX_THUMB_SIZE, self::MAX_THUMB_SIZE);
             self::_saveAsJPG($imagine, $thumbnail_file, $thumbnail_paths['thumbnail']['path'], 90, $thumbnail_size);
             $record->setThumbnail(self::_changeExtension($thumbnail_paths['thumbnail']['base'], 'jpg'));
         }
         // Delete the temp files (if not already moved).
         if ($submission_file) {
             @unlink($submission_file->getTempName());
         }
         if ($thumbnail_file) {
             @unlink($thumbnail_file->getTempName());
         }
         // Unhide the record that was hidden earlier.
         if (!$edit_mode) {
             $record->is_hidden = false;
         }
         $record->save();
         $view_url = $this->url->get('view/' . $record->id);
         $view_link = 'You can <a href="' . $view_url . '" target="_blank_">view your submission\'s public page here</a>.';
         if ($edit_mode) {
             $this->alert('<b>Submission Updated!</b><br>' . $view_link, 'green');
         } else {
             $this->alert('<b>New Submission Uploaded!</b><br>' . $view_link, 'green');
         }
         return $this->redirectFromHere(array('action' => 'index', 'id' => NULL, 'type' => NULL));
     }
     // Render the main form.
     $this->view->type_info = $type_info;
     $this->view->form = $form;
 }