public function createThali($dishArray = array(), $is_thali, $w = 140, $h = 0) { Configure::write('debug', 2); ini_set("max_execution_time", -1); $thali1 = new Imagick("tmpl/img/thali-1.png"); $thali2 = new Imagick("tmpl/img/thali-2.png"); $thali3 = new Imagick("tmpl/img/thali-3.png"); $mask_1 = new Imagick("tmpl/img/thali-mask2.png"); $mask_2 = new Imagick("tmpl/img/thali-mask3.png"); if (!is_array($dishArray)) { return false; } $mask_cnt = 0; foreach ($dishArray as $dish) { if ($mask_cnt > 1) { // Mask Locking (Modify if masks will be increased or decreased) break; } $dish = new Imagick($dish); $dish->scaleimage($thali1->getimagewidth(), $thali1->getimageheight()); // Set As per bowl image if ($is_thali) { if ($mask_cnt == 1) { $dish->rotateimage("#fff", 180); } $dish->compositeimage(new Imagick("tmpl/img/thali-mask" . ($mask_cnt + 2) . ".png"), \Imagick::COMPOSITE_COPYOPACITY, 0, 0, Imagick::CHANNEL_ALPHA); $dish->mergeimagelayers(Imagick::LAYERMETHOD_COALESCE); $thali1->compositeimage($dish, \Imagick::COMPOSITE_ATOP, 0, 0, Imagick::CHANNEL_ALPHA); $thali1->mergeimagelayers(Imagick::LAYERMETHOD_COALESCE); $thali2->compositeimage($dish, \Imagick::COMPOSITE_ATOP, 0, 0, Imagick::CHANNEL_ALPHA); $thali2->mergeimagelayers(Imagick::LAYERMETHOD_COALESCE); $thali3->compositeimage($dish, \Imagick::COMPOSITE_ATOP, 0, 0, Imagick::CHANNEL_ALPHA); $thali3->mergeimagelayers(Imagick::LAYERMETHOD_COALESCE); } else { $thali3 = $dish; } $mask_cnt++; } $url = "files/thali_images/" . $this->randomString(6); $url_end = "-Thali.jpg"; $result_urls = array(); $thali1->setimageformat("jpg"); $thali1->setImageFileName($result_urls[] = $url . "-0" . $url_end); $thali1->scaleimage($w, $h); if ($is_thali) { $thali2->writeimage(); } $thali1->destroy(); $thali2->setimageformat("jpg"); $thali2->setImageFileName($result_urls[] = $url . "-1" . $url_end); $thali2->scaleimage($w, $h); if ($is_thali) { $thali2->writeimage(); } $thali2->destroy(); $thali3->setimageformat("jpg"); $thali3->setImageFileName($result_urls[] = $url . "-2" . $url_end); $thali3->scaleimage($w, $h); $thali3->writeimage(); $thali3->destroy(); return $result_urls; }
/** * {@inheritdoc} * * @return ImageInterface */ public function rotate($angle, ColorInterface $background = null) { $color = $background ? $background : $this->palette->color('fff'); try { $pixel = $this->getColor($color); $this->imagick->rotateimage($pixel, $angle); $pixel->clear(); $pixel->destroy(); } catch (\ImagickException $e) { throw new RuntimeException('Rotate operation failed', $e->getCode(), $e); } return $this; }
/** * Apply transformations (rotate/crop) to an image * * ``` * $data = [ * 'rotate' => 90, * 'width' => 200, * 'height' => 300, * 'x' => 20, * 'y' => 50, * ]; * ``` * * @param \Cake\Datasource\EntityTrait $image the entity being transformed * @param array $data the transformation information * @return void */ public function transform(Entity $image, array $data = null) { $sourcePath = $image->sourcePath; $image->filename = uniqid() . '.' . $image->ext; $result = $this->_table->save($image); if ($result) { $imagick = new \Imagick($sourcePath); if (!is_null($data)) { $imagick->rotateimage('#000', $data['rotate']); $imagick->cropImage($data['width'], $data['height'], $data['x'], $data['y']); } $imagick->writeImage($result->sourcePath); unlink($sourcePath); } }
/** * Applies EXIF orientation metadata to pixel data and removes the EXIF rotation * * @access protected */ protected function autoOrient() { // apply EXIF orientation to pixel data switch ($this->originalImage->getImageOrientation()) { case \Imagick::ORIENTATION_BOTTOMRIGHT: $this->originalImage->rotateimage('#000', 180); // rotate 180 degrees break; case \Imagick::ORIENTATION_RIGHTTOP: $this->originalImage->rotateimage('#000', 90); // rotate 90 degrees CW break; case \Imagick::ORIENTATION_LEFTBOTTOM: $this->originalImage->rotateimage('#000', -90); // rotate 90 degrees CCW break; } // reset EXIF orientation $this->originalImage->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); }
static function fixImageRotationOld($file) { $degrees = NULL; $image = imagecreatefromstring(file_get_contents($file)); try { $exif = exif_read_data($file); } catch (Exception $exp) { return; } if ($exif) { $exif = \exif_read_data($file, 'ANY_TAG'); if (isset($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $degrees = 180; break; case 6: $degrees = 90; break; case 8: $degrees = -90; break; } } if (class_exists("\\Imagick")) { try { $img = new \Imagick($file); $img->stripImage(); if ($degrees) { $img->rotateimage("#ffffff", $degrees); } $img->writeImage($file); $img->clear(); $img->destroy(); } catch (Exception $e) { } } return; } }
private function gradientRight() { $imagick = new \Imagick(); $imagick->newpseudoimage($this->width, $this->height, 'gradient:black-white'); $imagick->rotateimage('black', -90); return $imagick; }
public function actionGo() { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { $f = $_POST['filter']; $capPhoto = $_POST['capPhoto']; $min_rand = rand(0, 1000); $max_rand = rand(100000000000, 10000000000000000); $name_file = rand($min_rand, $max_rand); //this part is for creating random name for image $ext = end(explode(".", $_FILES["file"]["name"])); //gets extension $file = Yii::app()->request->baseUrl . "photo/" . $name_file . "." . $ext; move_uploaded_file($_FILES["file"]["tmp_name"], Yii::app()->request->baseUrl . "photo/" . $name_file . "." . $ext); chmod($file, 0777); if (exif_imagetype($file) == IMAGETYPE_JPEG) { $exif = exif_read_data($file); if (isset($exif['Orientation'])) { $orientation = $exif['Orientation']; if ($orientation == 6) { $imz = new Imagick($file); $imz->rotateimage("#FFF", 90); $imz->writeImage($file); chmod($file, 0777); } else { if ($orientation == 8) { $imz = new Imagick($file); $imz->rotateimage("#FFF", -90); $imz->writeImage($file); chmod($file, 0777); } } } } // Max vert or horiz resolution $maxsize = 1200; // create new Imagick object $image = new Imagick($file); // Resizes to whichever is larger, width or height if ($image->getImageHeight() <= $image->getImageWidth()) { // Resize image using the lanczos resampling algorithm based on width $image->resizeImage($maxsize, 0, Imagick::FILTER_LANCZOS, 1); } else { // Resize image using the lanczos resampling algorithm based on height $image->resizeImage(0, $maxsize, Imagick::FILTER_LANCZOS, 1); } // Set to use jpeg compression $image->setImageCompression(Imagick::COMPRESSION_JPEG); // Set compression level (1 lowest quality, 100 highest quality) $image->setImageCompressionQuality(75); // Strip out unneeded meta data $image->stripImage(); // Writes resultant image to output directory $image->writeImage($file); // Destroys Imagick object, freeing allocated resources in the process $image->destroy(); chmod($file, 0777); $filter = Instagraph::factory($file, $file); $filter->{$f}(); // 320 Show Preview $immid = new Imagick($file); if ($immid->getimagewidth() > 320) { $immid->thumbnailImage(320, null); $immid->writeImage(Yii::app()->request->baseUrl . "thumb/thumb320_" . $name_file . "." . $ext); $immid->destroy(); chmod(Yii::app()->request->baseUrl . "thumb/thumb320_" . $name_file . "." . $ext, 0777); } else { $immid->writeImage(Yii::app()->request->baseUrl . "thumb/thumb320_" . $name_file . "." . $ext); $immid->destroy(); chmod(Yii::app()->request->baseUrl . "thumb/thumb320_" . $name_file . "." . $ext, 0777); } // null x 230 show last upload $imlast = new Imagick($file); $imlast->thumbnailimage(null, 230); $imlast->writeImage(Yii::app()->request->baseUrl . "thumb/thumb230_" . $name_file . "." . $ext); $imlast->destroy(); chmod(Yii::app()->request->baseUrl . "thumb/thumb230_" . $name_file . "." . $ext, 0777); // 130 x 110 thumbmail $im = new Imagick($file); $im->thumbnailImage(130, 110); $im->writeImage(Yii::app()->request->baseUrl . "thumb/thumb_" . $name_file . "." . $ext); chmod(Yii::app()->request->baseUrl . "thumb/thumb_" . $name_file . "." . $ext, 0777); $im->destroy(); $photo = new Photo(); $photo->link = $file; $photo->fbid = Yii::app()->facebook->getUser(); $photo->ip = $_SERVER['REMOTE_ADDR']; if ($photo->save()) { $id = $photo->id; if (isset($_POST['shareFB'])) { $share = 1; } else { $share = 0; } if ($share == 1) { $cr = "\n" . "http://www.pla2gram.com/?p=" . $id . "&theater=1"; $capFB = $capPhoto . $cr; // Post to Facebook $args = array('message' => $capFB); $args['image'] = '@' . realpath($file); Yii::app()->facebook->api('/me/photos', 'post', $args); Helper::redir("/?p=" . $id, 0); } else { Helper::redir("/?p=" . $id, 0); } } else { print_r($photo->getErrors()); } } }
/** * Rotate 90 degrees left * @param int $val * @return bool */ public function rotate($val = 90) { $source = $this->_getTempEditorFullPath(); $picture = new Imagick($source[0]); $picture->rotateimage(new ImagickPixel(), $val); $picture->writeImage($source[1]); $this->getRawEditorCache(); return true; }
public function _thumb($thumbPath, $maxWidth, $maxHeight) { try { $im = new \Imagick(); $im->readImage($this->path); switch ($this->orientation) { case \imagick::ORIENTATION_UNDEFINED: // 0 // 0 case \imagick::ORIENTATION_TOPLEFT: // 1 : no rotation break; case \imagick::ORIENTATION_BOTTOMRIGHT: // 3 : 180 deg $im->rotateimage(new \ImagickPixel(), 180); break; case \imagick::ORIENTATION_RIGHTTOP: // 6 : 90 deg CW $im->rotateimage(new \ImagickPixel(), 90); break; case \imagick::ORIENTATION_LEFTBOTTOM: // 8 : 90 deg CCW $im->rotateimage(new \ImagickPixel(), -90); break; default: // we should never get here, this is checked in load() as well die('Unsupported EXIF orientation'); } // Now that it's auto-rotated, make sure the EXIF data is correct, so // thumbnailImage doesn't try to autorotate the image $im->setImageOrientation(\imagick::ORIENTATION_TOPLEFT); if ($im->getImageWidth() > $maxWidth || $im->getImageHeight() > $maxHeight) { $im->thumbnailImage($maxWidth, $maxHeight, true); } $w = $im->getImageWidth(); $h = $im->getImageHeight(); $im->setImageCompressionQuality(75); $im->stripImage(); $im->writeImage($thumbPath); $im->destroy(); } catch (Exception $e) { return $e->getMessage(); # TODO: MAke sure this is handled! } return array($w, $h); }
/** * Automatic rotation fix * * @param \Imagick $image */ public function ImagickAutoRotateImage(\Imagick $image) { $orientation = $image->getImageOrientation(); switch ($orientation) { case \imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateimage("#000", 180); // rotate 180 degrees break; case \imagick::ORIENTATION_RIGHTTOP: $image->rotateimage("#000", 90); // rotate 90 degrees CW break; case \imagick::ORIENTATION_LEFTBOTTOM: $image->rotateimage("#000", -90); // rotate 90 degrees CCW break; } $image->setImageOrientation(\imagick::ORIENTATION_TOPLEFT); }
public static function thumbnail($item, $width = 0, $height = 0, $border = true, $ftname = '', $quality = 95, $scale_up_force = false) { if (is_numeric($item)) { $f = new file(); $f->load($item); $item = $f; } if (!get_class($item) == 'file') { return; } // precondition, the original image file must exist if (!file_exists($item->absolute_path()) || filesize($item->absolute_path()) < 1) { return; } $original = $item->absolute_path(); $thumbnail = ''; $item_id = $item->id; if (!empty($ftname)) { $item_id = $ftname; } else { if (!is_numeric($item_id)) { $item_id = md5($item->id); } } if ($border === true || $border === 'true' || $border === 1) { $border = 1; } else { $border = 0; } // do we have the thumbnail already created for this image? // option A) opaque JPEG FILE $thumbnail_path_jpg = NAVIGATE_PRIVATE . '/' . $item->website . '/thumbnails/' . $width . 'x' . $height . '-' . $border . '-' . $quality . '-' . $item_id . '.jpg'; if (file_exists($thumbnail_path_jpg)) { // ok, a file exists, but it's older than the image file? (original image file has changed) if (filemtime($thumbnail_path_jpg) > filemtime($original)) { // the thumbnail already exists and is up to date $thumbnail = $thumbnail_path_jpg; } } // option B) transparent PNG FILE $thumbnail_path_png = NAVIGATE_PRIVATE . '/' . $item->website . '/thumbnails/' . $width . 'x' . $height . '-' . $border . '-' . $item_id; if (file_exists($thumbnail_path_png)) { // ok, a file exists, but it's older than the image file? (original image file has changed) if (filemtime($thumbnail_path_png) > filemtime($original)) { // the thumbnail already exists and is up to date $thumbnail = $thumbnail_path_png; } } // do we have to create a new thumbnail if (empty($thumbnail) || isset($_GET['force']) || !(file_exists($thumbnail) && filesize($thumbnail) > 0)) { $thumbnail = $thumbnail_path_png; $handle = new upload($original); $size = array('width' => $handle->image_src_x, 'height' => $handle->image_src_y); $handle->image_convert = 'png'; $handle->file_max_size = '512M'; // maximum image size: 512M (it really depends on available memory) // if needed, calculate width or height with aspect ratio if (empty($width)) { if (!empty($size['height'])) { $width = round($height / $size['height'] * $size['width']); } else { $width = NULL; } return file::thumbnail($item, $width, $height, $border, $ftname); } else { if (empty($height)) { if (!empty($size['width'])) { $height = round($width / $size['width'] * $size['height']); } else { $height = NULL; } return file::thumbnail($item, $width, $height, $border, $ftname); } } $handle->image_x = $width; $handle->image_y = $height; if ($size['width'] < $width && $size['height'] < $height) { // the image size is under the requested width / height? => fill around with transparent color $handle->image_default_color = '#FFFFFF'; $handle->image_resize = true; $handle->image_ratio_no_zoom_in = true; $borderP = array(floor(($height - $size['height']) / 2), ceil(($width - $size['width']) / 2), ceil(($height - $size['height']) / 2), floor(($width - $size['width']) / 2)); $handle->image_border = $borderP; if ($scale_up_force) { $handle->image_border = array(); if ($height > width) { $handle->image_ratio_y = true; } else { $handle->image_ratio_x = true; } } $handle->image_border_color = '#FFFFFF'; $handle->image_border_opacity = 0; } else { // the image size is bigger than the requested width / height, we must resize it $handle->image_default_color = '#FFFFFF'; $handle->image_resize = true; $handle->image_ratio_fill = true; } if ($border == 0) { $handle->image_border = false; $handle->image_ratio_no_zoom_in = false; if (!empty($item->focalpoint) && $handle->image_src_x > 0) { $focalpoint = explode('#', $item->focalpoint); $crop = array('top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0); // calculate how the file will be scaled, by width or by height if ($handle->image_src_x / $handle->image_x > $handle->image_src_y / $handle->image_y) { // Y is ok, now crop extra space on X $ratio = $handle->image_y / $handle->image_src_y; $image_scaled_x = intval($handle->image_src_x * $ratio); $crop['left'] = max(0, round(($image_scaled_x * ($focalpoint[1] / 100) - $handle->image_x / 2) / $ratio)); $crop['right'] = max(0, round(($image_scaled_x * ((100 - $focalpoint[1]) / 100) - $handle->image_x / 2) / $ratio)); } else { // X is ok, now crop extra space on Y $ratio = $handle->image_x / $handle->image_src_x; $image_scaled_y = intval($handle->image_src_y * $ratio); $crop['top'] = max(0, round(($image_scaled_y * ($focalpoint[0] / 100) - $handle->image_y / 2) / $ratio)); $crop['bottom'] = max(0, round(($image_scaled_y * ((100 - $focalpoint[0]) / 100) - $handle->image_y / 2) / $ratio)); } $handle->image_precrop = array($crop['top'], $crop['right'], $crop['bottom'], $crop['left']); } $handle->image_ratio_crop = true; $handle->image_ratio_fill = true; } $handle->png_compression = 9; $handle->process(dirname($thumbnail)); rename($handle->file_dst_pathname, $thumbnail); clearstatcache(true, $thumbnail); if (!file_exists($thumbnail) || filesize($thumbnail) < 1) { return NULL; } // try to recompress the png thumbnail file to achieve the minimum file size, // only if some extra apps are available if (extension_loaded('imagick')) { $im = new Imagick($thumbnail); $image_alpha_range = $im->getImageChannelRange(Imagick::CHANNEL_ALPHA); //$image_alpha_mean = $im->getImageChannelMean(Imagick::CHANNEL_ALPHA); $image_is_opaque = $image_alpha_range['minima'] == 0 && $image_alpha_range['maxima'] == 0; // autorotate image based on EXIF data $im_original = new Imagick($original); $orientation = $im_original->getImageOrientation(); $im_original->clear(); switch ($orientation) { case imagick::ORIENTATION_BOTTOMRIGHT: $im->rotateimage(new ImagickPixel('transparent'), 180); // rotate 180 degrees break; case imagick::ORIENTATION_RIGHTTOP: $im->rotateimage(new ImagickPixel('transparent'), 90); // rotate 90 degrees CW break; case imagick::ORIENTATION_LEFTBOTTOM: $im->rotateimage(new ImagickPixel('transparent'), -90); // rotate 90 degrees CCW break; } // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! $im->setImageOrientation(imagick::ORIENTATION_TOPLEFT); if (!$image_is_opaque) { $im->setImageFormat('PNG32'); // Force a full RGBA image format with full semi-transparency. $im->setBackgroundColor(new ImagickPixel('transparent')); $im->setImageCompression(Imagick::COMPRESSION_UNDEFINED); $im->setImageCompressionQuality(0); $im->writeimage($thumbnail); } else { $im->setImageFormat('JPG'); // create an OPAQUE JPG file with the given quality (default 95%) $im->setImageCompressionQuality($quality); $im->writeimage($thumbnail_path_jpg); @unlink($thumbnail); $thumbnail = $thumbnail_path_jpg; } } /* if(command_exists('pngquant')) // PNG Optimization: 8 bit with transparency { @shell_exec('pngquant -s1 --ext .pngquant '.$thumbnail); if(file_exists($thumbnail.'.pngquant')) { unlink($thumbnail); rename($thumbnail.'.pngquant', $thumbnail); } } else*/ } clearstatcache(true, $thumbnail); return $thumbnail; }
protected function _rotatePhoto($photoData, $image, $angle, $tempFile) { if (XenForo_Application::getOptions()->imageLibrary['class'] === 'imPecl') { $class = new Imagick($image); $class->rotateimage(new ImagickPixel(), 360 - $angle); $class->setimageformat($photoData['extension']); $success = $class->writeImages($tempFile, true); $class->destroy(); } else { $gd = $this->createImageFromFile($image, self::$typeMap[$photoData['extension']]); if (!$gd) { return false; } $rotate = imagerotate($gd, $angle, imageColorAllocateAlpha($gd, 0, 0, 0, 127)); $success = $this->outputImage($rotate, self::$typeMap[$photoData['extension']], $tempFile); imagedestroy($rotate); imagedestroy($gd); } return $success; }
$fileExt = strtolower($fileExt); if (in_array($fileExt, array("png", "jpeg", "gif", "jpg"))) { $fileType = "image/" . $fileExt; } } } if (isset($fileType)) { try { $img = new Imagick($_FILES["imagefile"]["tmp_name"]); } catch (ImagickException $e) { return new Response("Invalid image", 500); } $orientation = $img->getImageOrientation(); switch ($orientation) { case imagick::ORIENTATION_BOTTOMRIGHT: $img->rotateimage("#000", 180); // rotate 180 degrees break; case imagick::ORIENTATION_RIGHTTOP: $img->rotateimage("#000", 90); // rotate 90 degrees CW break; case imagick::ORIENTATION_LEFTBOTTOM: $img->rotateimage("#000", -90); // rotate 90 degrees CCW break; } // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! $img->setImageOrientation(imagick::ORIENTATION_TOPLEFT); // Save originals when file type is gif // TODO resize GIFs
function init() { //generate image $width = 256; $height = 256; /* Instanciate and read the image in */ $im = new Imagick(base64_decode($_GET['uri'])); /* Fit the image into $width x $height box The third parameter fits the image into a "bounding box" */ $im->thumbnailImage($width, $height, true); /* Create a canvas with the desired color */ $canvas = new Imagick(); $canvas->newImage($width, $height, '#f2e6d8', 'png'); /* Get the image geometry */ $geometry = $im->getImageGeometry(); $im->rotateimage('#f2e6d8', 180); /* The overlay x and y coordinates */ $x = ($width - $geometry['width']) / 2; $y = ($height - $geometry['height']) / 2; /* Composite on the canvas */ $canvas->compositeImage($im, imagick::COMPOSITE_OVER, $x, $y); /* Output the image */ header("Content-Type: image/png"); echo $canvas; exit; }