static function fixImageRotation($file) { if (class_exists("\\Imagick")) { $image = new \Imagick($file); $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; } // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! $image->setImageOrientation(\imagick::ORIENTATION_TOPLEFT); $image->writeImage($file); } else { return false; } }
/** * Initiates new image from Imagick object * * @param Imagick $imagick * @return \Intervention\Image\Image */ public function initFromImagick(\Imagick $imagick) { // reset image orientation $imagick->setImageOrientation(\Imagick::ORIENTATION_UNDEFINED); // coalesce possible animation $imagick = $imagick->coalesceImages(); return new Image(new Driver(), new Container($imagick)); }
/** * @return $this */ public function clearAndApplyExifRotation() { $orientation = $this->_imagick->getImageOrientation(); switch ($orientation) { case Imagick::ORIENTATION_TOPRIGHT: // flipped // flipped case Imagick::ORIENTATION_UNDEFINED: // undefined // undefined case Imagick::ORIENTATION_TOPLEFT: // normal break; case Imagick::ORIENTATION_BOTTOMLEFT: // 180° flipped // 180° flipped case Imagick::ORIENTATION_BOTTOMRIGHT: // 180° $this->rotate(-180); $this->_imagick->setImageOrientation(1); break; case Imagick::ORIENTATION_LEFTTOP: // 270° flipped // 270° flipped case Imagick::ORIENTATION_RIGHTTOP: // 270° $this->rotate(-270); $this->_imagick->setImageOrientation(1); break; case Imagick::ORIENTATION_RIGHTBOTTOM: // 90° flipped // 90° flipped case Imagick::ORIENTATION_LEFTBOTTOM: // 90° $this->rotate(-90); $this->_imagick->setImageOrientation(1); break; } return $this; }
/** * 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); }
private function rotate_imagick($imagePath, $orientation) { $imagick = new Imagick($imagePath); $imagick->setImageOrientation(imagick::ORIENTATION_TOPLEFT); $deg = 0; switch ($orientation) { case 3: $deg = -180; break; case 6: $deg = 90; break; case 8: $deg = -90; break; } $imagick->rotateImage(new ImagickPixel('#00000000'), $deg); $imagick->writeImage($imagePath); $imagick->clear(); $imagick->destroy(); }
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); }
public function createMediaFromJSON($glob) { $path = $glob->path; $filename = $glob->filename; $data = $glob->data; $resizeTo = isset($glob->resizeTo) ? $glob->resizeTo : null; $gameMediaDirectory = Media::getMediaDirectory($path)->data; $md5 = md5((string) microtime() . $filename); $ext = strtolower(substr($filename, -3)); $newMediaFileName = 'aris' . $md5 . '.' . $ext; $resizedMediaFileName = 'aris' . $md5 . '_128.' . $ext; if ($ext != "jpg" && $ext != "png" && $ext != "gif" && $ext != "mp4" && $ext != "mov" && $ext != "m4v" && $ext != "3gp" && $ext != "caf" && $ext != "mp3" && $ext != "aac" && $ext != "m4a" && $ext != "zip") { return new returnData(1, NULL, "Invalid filetype:{$ext}"); } $fullFilePath = $gameMediaDirectory . "/" . $newMediaFileName; if (isset($resizeTo) && ($ext == "jpg" || $ext == "png" || $ext == "gif")) { $bigFilePath = $gameMediaDirectory . "/big_" . $newMediaFileName; $fp = fopen($bigFilePath, 'w'); if (!$fp) { return new returnData(1, NULL, "Couldn't open file:{$bigFilePath}"); } fwrite($fp, base64_decode($data)); fclose($fp); $image = new Imagick($bigFilePath); // Reorient based on EXIF tag switch ($image->getImageOrientation()) { case Imagick::ORIENTATION_UNDEFINED: // We assume normal orientation break; case Imagick::ORIENTATION_TOPLEFT: // All good break; case Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage('#000', 180); break; case Imagick::ORIENTATION_BOTTOMLEFT: $image->rotateImage('#000', 180); $image->flopImage(); break; case Imagick::ORIENTATION_LEFTTOP: $image->rotateImage('#000', 90); $image->flopImage(); break; case Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage('#000', 90); break; case Imagick::ORIENTATION_RIGHTBOTTOM: $image->rotateImage('#000', -90); $image->flopImage(); break; case Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage('#000', -90); break; } $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); // Resize image proportionally so min(width, height) == $resizeTo if ($image->getImageWidth() < $image->getImageHeight()) { $image->resizeImage($resizeTo, 0, Imagick::FILTER_LANCZOS, 1); } else { $image->resizeImage(0, $resizeTo, Imagick::FILTER_LANCZOS, 1); } $image->setImageCompression(Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality(40); $image->writeImage($fullFilePath); unlink($bigFilePath); } else { $fp = fopen($fullFilePath, 'w'); if (!$fp) { return new returnData(1, NULL, "Couldn't open file:{$fullFilePath}"); } fwrite($fp, base64_decode($data)); fclose($fp); } if ($ext == "jpg" || $ext == "png" || $ext == "gif") { $img = WideImage::load($fullFilePath); $img = $img->resize(128, 128, 'outside'); $img = $img->crop('center', 'center', 128, 128); $img->saveToFile($gameMediaDirectory . "/" . $resizedMediaFileName); } else { if ($ext == "mp4") { /* $ffmpeg = '../../libraries/ffmpeg'; $videoFilePath = $gameMediaDirectory."/".$newMediaFileName; $tempImageFilePath = $gameMediaDirectory."/temp_".$resizedMediaFileName; $imageFilePath = $gameMediaDirectory."/".$resizedMediaFileName; $cmd = "$ffmpeg -i $videoFilePath 2>&1"; $thumbTime = 1; if(preg_match('/Duration: ((\d+):(\d+):(\d+))/s', shell_exec($cmd), $videoLength)) $thumbTime = (($videoLength[2] * 3600) + ($videoLength[3] * 60) + $videoLength[4])/2; $cmd = "$ffmpeg -i $videoFilePath -deinterlace -an -ss $thumbTime -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $tempImageFilePath 2>&1"; shell_exec($cmd); $img = WideImage::load($tempImageFilePath); $img = $img->resize(128, 128, 'outside'); $img = $img->crop('center','center',128,128); $img->saveToFile($imageFilePath); */ } } $m = Media::createMedia($path, "UploadedMedia", $newMediaFileName, 0); return new returnData(0, $m->data); }
function exif($file) { $file_parts = pathinfo($file['file']); if (in_array(strtolower($file_parts['extension']), array('jpg', 'jpeg', 'tiff'))) { $exif = @read_exif_data($file['file']); $exif_orient = isset($exif['Orientation']) ? $exif['Orientation'] : 0; $rotate_image = 0; $exif_orient = intval($exif_orient); if (6 === intval($exif_orient)) { $rotate_image = 90; $image_orientation = 1; } elseif (3 === $exif_orient) { $rotate_image = 180; $image_orientation = 1; } elseif (8 === $exif_orient) { $rotate_image = 270; $image_orientation = 1; } if ($rotate_image) { if (class_exists('Imagick')) { $imagick = new Imagick(); $imagick->readImage($file['file']); $imagick->rotateImage(new ImagickPixel(), $rotate_image); $imagick->setImageOrientation($image_orientation); $imagick->writeImage($file['file']); $imagick->clear(); $imagick->destroy(); } else { $rotate_image = -$rotate_image; switch ($file['type']) { case 'image/jpeg': $source = imagecreatefromjpeg($file['file']); $rotate = imagerotate($source, $rotate_image, 0); imagejpeg($rotate, $file['file']); break; case 'image/png': $source = imagecreatefrompng($file['file']); $rotate = imagerotate($source, $rotate_image, 0); imagepng($rotate, $file['file']); break; case 'image/gif': $source = imagecreatefromgif($file['file']); $rotate = imagerotate($source, $rotate_image, 0); imagegif($rotate, $file['file']); break; default: break; } } } } return $file; }
/** * 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; }
function imgRotate($imgFile) { $imagick = new \Imagick(); $imagick->readImage($imgFile); $format = strtolower($imagick->getImageFormat()); if ($format === 'jpeg') { $orientation = $imagick->getImageOrientation(); $isRotated = false; if ($orientation === \Imagick::ORIENTATION_RIGHTTOP) { $imagick->rotateImage('none', 90); $isRotated = true; } elseif ($orientation === \Imagick::ORIENTATION_BOTTOMRIGHT) { $imagick->rotateImage('none', 180); $isRotated = true; } elseif ($orientation === \Imagick::ORIENTATION_LEFTBOTTOM) { $imagick->rotateImage('none', 270); $isRotated = true; } if ($isRotated) { $imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); } } }
/** * Fixes image orientation. * * @since 1.0.0 * @package GeoDirectory * @param array $file The image file. * @return mixed Image file. */ function geodir_exif($file) { //This line reads the EXIF data and passes it into an array $file['file'] = $file['tmp_name']; if ($file['type'] == "image/jpg" || $file['type'] == "image/jpeg" || $file['type'] == "image/pjpeg") { } else { return $file; } if (!function_exists('read_exif_data')) { return $file; } $exif = read_exif_data($file['file']); //We're only interested in the orientation $exif_orient = isset($exif['Orientation']) ? $exif['Orientation'] : 0; $rotateImage = 0; //We convert the exif rotation to degrees for further use if (6 == $exif_orient) { $rotateImage = 90; $imageOrientation = 1; } elseif (3 == $exif_orient) { $rotateImage = 180; $imageOrientation = 1; } elseif (8 == $exif_orient) { $rotateImage = 270; $imageOrientation = 1; } //if the image is rotated if ($rotateImage) { //WordPress 3.5+ have started using Imagick, if it is available since there is a noticeable difference in quality //Why spoil beautiful images by rotating them with GD, if the user has Imagick if (class_exists('Imagick')) { $imagick = new Imagick(); $imagick->readImage($file['file']); $imagick->rotateImage(new ImagickPixel(), $rotateImage); $imagick->setImageOrientation($imageOrientation); $imagick->writeImage($file['file']); $imagick->clear(); $imagick->destroy(); } else { //if no Imagick, fallback to GD //GD needs negative degrees $rotateImage = -$rotateImage; switch ($file['type']) { case 'image/jpeg': $source = imagecreatefromjpeg($file['file']); $rotate = imagerotate($source, $rotateImage, 0); imagejpeg($rotate, $file['file']); break; case 'image/png': $source = imagecreatefrompng($file['file']); $rotate = imagerotate($source, $rotateImage, 0); imagepng($rotate, $file['file']); break; case 'image/gif': $source = imagecreatefromgif($file['file']); $rotate = imagerotate($source, $rotateImage, 0); imagegif($rotate, $file['file']); break; default: break; } } } // The image orientation is fixed, pass it back for further processing return $file; }
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 if ($fileType == "image/gif") { $sizes[0] = true; $sizes[7] = false; } // Save Originals if ($sizes[0]) { $files[0] = fopen($_FILES["imagefile"]["tmp_name"], "r"); $fileNames[0] = $filePrefix . "." . $fileExt; } } } if (isset($img)) { $useZga = false;
/** * rotate image depending on exif info * * @param Imagick $image image handler * @return void */ protected function _autorotate(\Imagick $image) { switch ($image->getImageOrientation()) { case \Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case \Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage('#000', 180); break; case \Imagick::ORIENTATION_BOTTOMLEFT: $image->flopImage(); $image->rotateImage('#000', 180); break; case \Imagick::ORIENTATION_LEFTTOP: $image->flopImage(); $image->rotateImage('#000', -90); break; case \Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage('#000', 90); break; case \Imagick::ORIENTATION_RIGHTBOTTOM: $image->flopImage(); $image->rotateImage('#000', 90); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage('#000', -90); break; } $image->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); }
/** * Internal * * Applies options before save or output * * @param \Imagick $image * @param array $options */ private function applyImageOptions(\Imagick $image, array $options) { if (isset($options['quality'])) { $image->setImageCompressionQuality($options['quality']); } if (isset($options['orientation']) && intval($options['orientation']) > 0) { $image->setImageOrientation($options['orientation']); } if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) { if ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) { $image->setImageUnits(\Imagick::RESOLUTION_PIXELSPERCENTIMETER); } elseif ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERINCH) { $image->setImageUnits(\Imagick::RESOLUTION_PIXELSPERINCH); } else { throw new RuntimeException('Unsupported image unit format'); } $image->setImageResolution($options['resolution-x'], $options['resolution-y']); $image->resampleImage($options['resolution-x'], $options['resolution-y'], \Imagick::FILTER_UNDEFINED, 0); } }
/** * Rotates the specified image based on the EXIF meta data. * * @param Imagick $imagick The image object * @return Imagick The rotated image object */ private function rotateImage(\Imagick $imagick) { $orientation = $imagick->getImageOrientation(); switch ($orientation) { case \Imagick::ORIENTATION_BOTTOMRIGHT: $imagick->rotateImage("#000000", 180); break; case \Imagick::ORIENTATION_RIGHTTOP: $imagick->rotateImage("#000000", 90); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $imagick->rotateImage("#000000", -90); break; } $imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); return $imagick; }
protected function imagick_orient_image(\Imagick $image) { $orientation = $image->getImageOrientation(); $background = new \ImagickPixel('none'); switch ($orientation) { case \imagick::ORIENTATION_TOPRIGHT: // 2 $image->flopImage(); // horizontal flop around y-axis break; case \imagick::ORIENTATION_BOTTOMRIGHT: // 3 $image->rotateImage($background, 180); break; case \imagick::ORIENTATION_BOTTOMLEFT: // 4 $image->flipImage(); // vertical flip around x-axis break; case \imagick::ORIENTATION_LEFTTOP: // 5 $image->flopImage(); // horizontal flop around y-axis $image->rotateImage($background, 270); break; case \imagick::ORIENTATION_RIGHTTOP: // 6 $image->rotateImage($background, 90); break; case \imagick::ORIENTATION_RIGHTBOTTOM: // 7 $image->flipImage(); // vertical flip around x-axis $image->rotateImage($background, 270); break; case \imagick::ORIENTATION_LEFTBOTTOM: // 8 $image->rotateImage($background, 270); break; default: return false; } $image->setImageOrientation(\imagick::ORIENTATION_TOPLEFT); // 1 return true; }
public static function createMedia($pack) { $pack->auth->permission = "read_write"; if (!users::authenticateUser($pack->auth)) { return new return_package(6, NULL, "Failed Authentication"); } $filenameext = strtolower(substr($pack->file_name, strrpos($pack->file_name, '.') + 1)); if ($filenameext == "jpeg") { $filenameext = "jpg"; } //sanity $filename = md5((string) microtime() . $pack->file_name); $newfilename = 'aris' . $filename . '.' . $filenameext; $resizedfilename = 'aris' . $filename . '_resized.' . $filenameext; $newthumbfilename = 'aris' . $filename . '_128.' . $filenameext; // Make sure playerUploaded requirements keep in sync with this list if ($filenameext != "jpg" && $filenameext != "png" && $filenameext != "gif" && $filenameext != "mp4" && $filenameext != "mov" && $filenameext != "m4v" && $filenameext != "3gp" && $filenameext != "caf" && $filenameext != "mp3" && $filenameext != "aac" && $filenameext != "m4a") { return new return_package(1, NULL, "Invalid filetype: '{$filenameext}'"); } $filefolder = ""; if ($pack->game_id) { $filefolder = $pack->game_id; } else { $filefolder = "players"; } $fspath = Config::v2_gamedata_folder . "/" . $filefolder . "/" . $newfilename; $resizedpath = Config::v2_gamedata_folder . "/" . $filefolder . "/" . $resizedfilename; $fsthumbpath = Config::v2_gamedata_folder . "/" . $filefolder . "/" . $newthumbfilename; $fp = fopen($fspath, 'w'); if (!$fp) { return new return_package(1, NULL, "Couldn't open file:{$fspath}"); } fwrite($fp, base64_decode($pack->data)); fclose($fp); $did_resize = false; if ($filenameext == "jpg" || $filenameext == "png" || $filenameext == "gif") { if (isset($pack->resize)) { $image = new Imagick($fspath); // Reorient based on EXIF tag switch ($image->getImageOrientation()) { case Imagick::ORIENTATION_UNDEFINED: // We assume normal orientation break; case Imagick::ORIENTATION_TOPLEFT: // All good break; case Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage('#000', 180); break; case Imagick::ORIENTATION_BOTTOMLEFT: $image->rotateImage('#000', 180); $image->flopImage(); break; case Imagick::ORIENTATION_LEFTTOP: $image->rotateImage('#000', 90); $image->flopImage(); break; case Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage('#000', 90); break; case Imagick::ORIENTATION_RIGHTBOTTOM: $image->rotateImage('#000', -90); $image->flopImage(); break; case Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage('#000', -90); break; } $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); // Resize image proportionally so min(width, height) == $pack->resize $w = $image->getImageWidth(); $h = $image->getImageHeight(); if ($w < $h) { $image->resizeImage($pack->resize, $pack->resize / $w * $h, Imagick::FILTER_LANCZOS, 1); } else { $image->resizeImage($pack->resize / $h * $w, $pack->resize, Imagick::FILTER_LANCZOS, 1); } $image->setImageCompression(Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality(40); $image->writeImage($resizedpath); $did_resize = true; } $image = new Imagick(isset($pack->resize) ? $resizedpath : $fspath); //aspect fill to 128x128 $w = $image->getImageWidth(); $h = $image->getImageHeight(); if ($w < $h) { $image->thumbnailImage(128, 128 / $w * $h, 1, 1); } else { $image->thumbnailImage(128 / $h * $w, 128, 1, 1); } //crop around center $w = $image->getImageWidth(); $h = $image->getImageHeight(); $image->cropImage(128, 128, ($w - 128) / 2, ($h - 128) / 2); $image->writeImage($fsthumbpath); } if ($did_resize) { unlink($fspath); } // after making the 128 thumbnail $pack->media_id = dbconnection::queryInsert("INSERT INTO media (" . "file_folder," . "file_name," . (isset($pack->game_id) ? "game_id," : "") . (isset($pack->auth->user_id) ? "user_id," : "") . (isset($pack->name) ? "name," : "") . "created" . ") VALUES (" . "'" . $filefolder . "'," . "'" . ($did_resize ? $resizedfilename : $newfilename) . "'," . (isset($pack->game_id) ? "'" . addslashes($pack->game_id) . "'," : "") . (isset($pack->auth->user_id) ? "'" . addslashes($pack->auth->user_id) . "'," : "") . (isset($pack->name) ? "'" . addslashes($pack->name) . "'," : "") . "CURRENT_TIMESTAMP" . ")"); games::bumpGameVersion($pack); return media::getMedia($pack); }
/** * @since 3.0.2 */ function awpcp_rotate_image_with_imagick($filepath, $angle) { $imagick = new Imagick(); $imagick->readImage($filepath); $imagick->rotateImage(new ImagickPixel(), $angle); $imagick->setImageOrientation(1); $imagick->writeImage($filepath); $imagick->clear(); $imagick->destroy(); }
public function adjustFile($path, $info) { # Check dependencies self::dependencies(isset($path, $info)); # Call plugins $this->plugins(__METHOD__, 0, func_get_args()); if (extension_loaded('imagick') && $this->settings['imagick'] === '1') { $rotateImage = 0; switch ($info['orientation']) { case 3: $rotateImage = 180; $imageOrientation = 1; break; case 6: $rotateImage = 90; $imageOrientation = 1; break; case 8: $rotateImage = 270; $imageOrientation = 1; break; } if ($rotateImage !== 0) { $image = new Imagick(); $image->readImage($path); $image->rotateImage(new ImagickPixel(), $rotateImage); $image->setImageOrientation($imageOrientation); $image->writeImage($path); $image->clear(); $image->destroy(); } } else { $newWidth = $info['width']; $newHeight = $info['height']; $process = false; $sourceImg = imagecreatefromjpeg($path); switch ($info['orientation']) { case 2: # mirror # not yet implemented break; case 3: $process = true; $sourceImg = imagerotate($sourceImg, -180, 0); break; case 4: # rotate 180 and mirror # not yet implemented break; case 5: # rotate 90 and mirror # not yet implemented break; case 6: $process = true; $sourceImg = imagerotate($sourceImg, -90, 0); $newWidth = $info['height']; $newHeight = $info['width']; break; case 7: # rotate -90 and mirror # not yet implemented break; case 8: $process = true; $sourceImg = imagerotate($sourceImg, 90, 0); $newWidth = $info['height']; $newHeight = $info['width']; break; } # Need to adjust photo? if ($process === true) { # Recreate photo $newSourceImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); imagejpeg($newSourceImg, $path, 100); # Free memory imagedestroy($sourceImg); imagedestroy($newSourceImg); } } # Call plugins $this->plugins(__METHOD__, 1, func_get_args()); return true; }
/** * Auto-correction of image orientation based on exif data * * @param array $data */ protected function OrientationCorrection($data) { // catch for those who don't have exif module loaded if (!is_callable('exif_read_data')) { return; } $exif = exif_read_data($data['tmp_name']); $orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 0; if ($orientation != 0 || $orientation != 1) { $imageLib = elgg_get_plugin_setting('image_lib', 'tidypics'); if ($imageLib == 'ImageMagick') { // ImageMagick command line $im_path = elgg_get_plugin_setting('im_path', 'tidypics'); if (!$im_path) { $im_path = "/usr/bin/"; } if (substr($im_path, strlen($im_path) - 1, 1) != "/") { $im_path .= "/"; } $filename = $data['tmp_name']; $command = $im_path . "mogrify -auto-orient {$filename}"; $output = array(); $ret = 0; exec($command, $output, $ret); } else { if ($imageLib == 'ImageMagickPHP') { // imagick php extension $rotate = false; $flop = false; $angle = 0; switch ($orientation) { case 2: $rotate = false; $flop = true; break; case 3: $rotate = true; $flop = false; $angle = 180; break; case 4: $rotate = true; $flop = true; $angle = 180; break; case 5: $rotate = true; $flop = true; $angle = 90; break; case 6: $rotate = true; $flop = false; $angle = 90; break; case 7: $rotate = true; $flop = true; $angle = -90; break; case 8: $rotate = true; $flop = false; $angle = -90; break; default: $rotate = false; $flop = false; break; } $imagick = new Imagick(); $imagick->readImage($data['tmp_name']); if ($rotate) { $imagick->rotateImage('#000000', $angle); } if ($flop) { $imagick->flopImage(); } $imagick->setImageOrientation(imagick::ORIENTATION_TOPLEFT); $imagick->writeImage($data['tmp_name']); $imagick->clear(); $imagick->destroy(); } else { // make sure the in memory image size does not exceed memory available $imginfo = getimagesize($data['tmp_name']); $requiredMemory1 = ceil($imginfo[0] * $imginfo[1] * 5.35); $requiredMemory2 = ceil($imginfo[0] * $imginfo[1] * ($imginfo['bits'] / 8) * $imginfo['channels'] * 2.5); $requiredMemory = (int) max($requiredMemory1, $requiredMemory2); $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($requiredMemory < $mem_avail) { $image = imagecreatefromstring(file_get_contents($data['tmp_name'])); $rotate = false; $flip = false; $angle = 0; switch ($orientation) { case 2: $rotate = false; $flip = true; break; case 3: $rotate = true; $flip = false; $angle = 180; break; case 4: $rotate = true; $flip = true; $angle = 180; break; case 5: $rotate = true; $flip = true; $angle = -90; break; case 6: $rotate = true; $flip = false; $angle = -90; break; case 7: $rotate = true; $flip = true; $angle = 90; break; case 8: $rotate = true; $flip = false; $angle = 90; break; default: $rotate = false; $flip = false; break; } if ($rotate) { $image = imagerotate($image, $angle, 0); imagejpeg($image, $data['tmp_name']); } if ($flip) { $mem_avail = elgg_get_ini_setting_in_bytes('memory_limit'); $mem_used = memory_get_usage(); $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer if ($requiredMemory < $mem_avail) { $width = imagesx($image); $height = imagesy($image); $src_x = 0; $src_y = 0; $src_width = $width; $src_height = $height; $src_x = $width - 1; $src_width = -$width; $imgdest = imagecreatetruecolor($width, $height); imagecopyresampled($imgdest, $image, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height); imagejpeg($imgdest, $data['tmp_name']); imagedestroy($imgdest); } } imagedestroy($image); } } } } }
public function adjustFile($path, $info) { # Function rotates and flips a photo based on its EXIF orientation # Excepts the following: # (string) $path = Path to the photo-file # (array) $info = ['orientation', 'width', 'height'] # Returns the following # (array) $info = ['orientation', 'width', 'height'] = Success # (boolean) false = Failure # Check dependencies self::dependencies(isset($path, $info)); # Call plugins $this->plugins(__METHOD__, 0, func_get_args()); $swapSize = false; if (extension_loaded('imagick') && $this->settings['imagick'] === '1') { $rotateImage = 0; switch ($info['orientation']) { case 3: $rotateImage = 180; break; case 6: $rotateImage = 90; $swapSize = true; break; case 8: $rotateImage = 270; $swapSize = true; break; default: return false; break; } if ($rotateImage !== 0) { $image = new Imagick(); $image->readImage($path); $image->rotateImage(new ImagickPixel(), $rotateImage); $image->setImageOrientation(1); $image->writeImage($path); $image->clear(); $image->destroy(); } } else { $newWidth = $info['width']; $newHeight = $info['height']; $sourceImg = imagecreatefromjpeg($path); switch ($info['orientation']) { case 2: # mirror # not yet implemented return false; break; case 3: $process = true; $sourceImg = imagerotate($sourceImg, -180, 0); break; case 4: # rotate 180 and mirror # not yet implemented return false; break; case 5: # rotate 90 and mirror # not yet implemented return false; break; case 6: $process = true; $sourceImg = imagerotate($sourceImg, -90, 0); $newWidth = $info['height']; $newHeight = $info['width']; $swapSize = true; break; case 7: # rotate -90 and mirror # not yet implemented return false; break; case 8: $process = true; $sourceImg = imagerotate($sourceImg, 90, 0); $newWidth = $info['height']; $newHeight = $info['width']; $swapSize = true; break; default: return false; break; } # Recreate photo $newSourceImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); imagejpeg($newSourceImg, $path, 100); # Free memory imagedestroy($sourceImg); imagedestroy($newSourceImg); } # Call plugins $this->plugins(__METHOD__, 1, func_get_args()); # SwapSize should be true when the image has been rotated # Return new dimensions in this case if ($swapSize === true) { $swapSize = $info['width']; $info['width'] = $info['height']; $info['height'] = $swapSize; } return $info; }
/** * Strip orientation from EXIF data for an image at a path. * * @param $filePath * * @return bool */ public function stripOrientationFromExifData($filePath) { if (!ImageHelper::canHaveExifData($filePath)) { return null; } // Quick and dirty, if possible if ($this->isImagick() && method_exists('Imagick', 'setImageProperty')) { $image = new \Imagick($filePath); $image->setImageOrientation(\Imagick::ORIENTATION_UNDEFINED); $image->writeImages($filePath, true); return true; } $data = new PelDataWindow(IOHelper::getFileContents($filePath)); // Is this a valid JPEG? if (PelJpeg::isValid($data)) { $jpeg = $file = new PelJpeg(); $jpeg->load($data); $exif = $jpeg->getExif(); if ($exif) { $tiff = $exif->getTiff(); $ifd0 = $tiff->getIfd(); // Delete the Orientation entry and re-save the file $ifd0->offsetUnset(PelTag::ORIENTATION); $file->saveFile($filePath); return true; } } return false; }