/** * 入力された画像がExifであればエラーを記録し、JFIFに変換します。 * @param \Imagick $imagick JFIFかExif。 */ protected function convertExifToJFIF(\Imagick $imagick) { if ($imagick->getImageProperties('exif:*')) { $this->logger->error(sprintf(_('「%s」はExif形式です。'), $this->filename)); switch ($imagick->getImageOrientation()) { case \Imagick::ORIENTATION_TOPRIGHT: $imagick->flopImage(); break; case \Imagick::ORIENTATION_BOTTOMRIGHT: $imagick->rotateImage('none', 180); break; case \Imagick::ORIENTATION_BOTTOMLEFT: $imagick->rotateImage('none', 180); $imagick->flopImage(); break; case \Imagick::ORIENTATION_LEFTTOP: $imagick->rotateImage('none', 90); $imagick->flopImage(); break; case \Imagick::ORIENTATION_RIGHTTOP: $imagick->rotateImage('none', 90); break; case \Imagick::ORIENTATION_RIGHTBOTTOM: $imagick->rotateImage('none', 270); $imagick->flopImage(); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $imagick->rotateImage('none', 270); break; } $imagick->stripImage(); } }
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; } }
/** * @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; }
/** * @param Asset $asset * @return string */ public function handleSave(Asset $asset) { $newImage = new \Imagick($asset->getUploadedFile()->getRealPath()); $asset->setHeight($newImage->getImageHeight()); $asset->setWidth($newImage->getImageWidth()); /** @var array $requiredImage */ foreach ($this->requiredImages as $requiredImage) { $newImage = new \Imagick($asset->getUploadedFile()->getRealPath()); $newFilename = pathinfo($asset->getFilename(), PATHINFO_FILENAME) . '-' . $requiredImage['name'] . '.' . pathinfo($asset->getFilename(), PATHINFO_EXTENSION); $imageWidth = $newImage->getImageWidth(); $imageHeight = $newImage->getImageHeight(); $isLandscapeFormat = $imageWidth > $imageHeight; $orientation = $newImage->getImageOrientation(); $newImage->setCompression(\Imagick::COMPRESSION_JPEG); $newImage->setImageCompressionQuality($requiredImage['quality']); if ($isLandscapeFormat) { $desiredWidth = $requiredImage['long']; $newImage->resizeImage($desiredWidth, 0, \Imagick::FILTER_LANCZOS, 1); if (isset($requiredImage['short']) && boolval($requiredImage['crop']) == true) { $newImage->cropImage($desiredWidth, $requiredImage['short'], 0, 0); } else { $newImage->resizeImage(0, $requiredImage['short'], \Imagick::FILTER_LANCZOS, 1); } } else { $desiredHeight = $requiredImage['long']; $newImage->resizeImage(0, $desiredHeight, \Imagick::FILTER_LANCZOS, 1); if (isset($requiredImage['short']) && boolval($requiredImage['crop']) == true) { $newImage->cropImage($requiredImage['short'], $desiredHeight, 0, 0); } else { $newImage->resizeImage($requiredImage['short'], 0, \Imagick::FILTER_LANCZOS, 1); } } /** * This unfortunately kills the orientation. Leave EXIF-Info for now. * * $newImage->stripImage(); * $newImage->setImageOrientation($orientation); */ $this->assetStorage->uploadFile($newFilename, $newImage); $subAsset = new SubAsset(); $subAsset->setFilename($newFilename); $subAsset->setType($requiredImage['name']); $subAsset->setHeight($newImage->getImageHeight()); $subAsset->setWidth($newImage->getImageWidth()); $asset->addSubAsset($subAsset); } }
/** * @ref http://www.b-prep.com/blog/?p=1764 * * @param \Imagick $image * @return \Imagick */ private function fixImageOrientation($image) { $orientation = $image->getImageOrientation(); switch ($orientation) { case \Imagick::ORIENTATION_UNDEFINED: break; case \Imagick::ORIENTATION_TOPLEFT: break; case \Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage(new \ImagickPixel(), 180); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_BOTTOMLEFT: $image->rotateImage(new \ImagickPixel(), 180); $image->flopImage(); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_LEFTTOP: $image->rotateImage(new \ImagickPixel(), 90); $image->flopImage(); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage(new \ImagickPixel(), 90); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_RIGHTBOTTOM: $image->rotateImage(new \ImagickPixel(), 270); $image->flopImage(); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage(new \ImagickPixel(), 270); $image->setimageorientation(\Imagick::ORIENTATION_TOPLEFT); break; } return $image; }
/** * 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); }
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); }
/** * 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); } } }
/** * 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); }
private function _getImageInfo() { $imageinfo = NULL; // if($size = @getimagesize($this->tmpfile)) { // if($size[0] > 0 && $size[1] > 0) { // $imageinfo['width'] = $size[0]; // $imageinfo['height'] = $size[1]; // //$imageinfo['image_type']=$size[2]; // } // } if (!$imageinfo && class_exists('Imagick')) { $img = new Imagick($this->tmpfile); $imageinfo['width'] = $img->getImageWidth(); $imageinfo['height'] = $img->getImageHeight(); try { $imageinfo['orientation'] = $img->getImageOrientation(); } catch (ImagickException $e) { $imageinfo['orientation'] = 0; } //$imageinfo['image_type']=@exif_imagetype($this->tmpfile); } return $imageinfo; }
/** * 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); }
/** * Corrects orientation of an image * @param \Imagick $im */ private function correctOrientation(\Imagick &$im) { switch ($im->getImageOrientation()) { case \Imagick::ORIENTATION_TOPRIGHT: $im->flipImage(); break; case \Imagick::ORIENTATION_BOTTOMRIGHT: $im->rotateImage(new \ImagickPixel("none"), 180); break; case \Imagick::ORIENTATION_BOTTOMLEFT: $im->rotateImage(new \ImagickPixel("none"), 180); $im->flipImage(); break; case \Imagick::ORIENTATION_LEFTTOP: $im->rotateImage(new \ImagickPixel("none"), 90); $im->flipImage(); break; case \Imagick::ORIENTATION_RIGHTTOP: $im->rotateImage(new \ImagickPixel("none"), 90); break; case \Imagick::ORIENTATION_RIGHTBOTTOM: $im->rotateImage(new \ImagickPixel("none"), -90); $im->flipImage(); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $im->rotateImage(new \ImagickPixel("none"), -90); break; default: } }
if ($_FILES["imagefile"]["type"] == "application/octet-stream") { $fileExt = explode(".", $_FILES["imagefile"]["name"]); $fileExt = array_pop($fileExt); $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!