function testWriteRead() { Pel::setStrictParsing(true); $ifd = new PelIfd(PelIfd::IFD0); $this->assertTrue($ifd->isLastIfd()); foreach ($this->entries as $entry) { $ifd->addEntry($entry); } $tiff = new PelTiff(); $this->assertNull($tiff->getIfd()); $tiff->setIfd($ifd); $this->assertNotNull($tiff->getIfd()); $exif = new PelExif(); $this->assertNull($exif->getTiff()); $exif->setTiff($tiff); $this->assertNotNull($exif->getTiff()); $jpeg = new PelJpeg(dirname(__FILE__) . '/no-exif.jpg'); $this->assertNull($jpeg->getExif()); $jpeg->setExif($exif); $this->assertNotNull($jpeg->getExif()); $jpeg->saveFile('test-output.jpg'); $this->assertTrue(file_exists('test-output.jpg')); $this->assertTrue(filesize('test-output.jpg') > 0); /* Now read the file and see if the entries are still there. */ $jpeg = new PelJpeg('test-output.jpg'); $exif = $jpeg->getExif(); $this->assertIsA($exif, 'PelExif'); $tiff = $exif->getTiff(); $this->assertIsA($tiff, 'PelTiff'); $ifd = $tiff->getIfd(); $this->assertIsA($ifd, 'PelIfd'); $this->assertEqual($ifd->getType(), PelIfd::IFD0); $this->assertTrue($ifd->isLastIfd()); foreach ($this->entries as $entry) { $this->assertEqual($ifd->getEntry($entry->getTag())->getValue(), $entry->getValue()); } unlink('test-output.jpg'); }
/** * 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; } $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; } else { return false; } }
private function _addExif($filename) { //if ($_SERVER['SERVER_NAME'] == 'test.psychomorph') return true; try { $jpeg = new PelJpeg($filename); if (!($exif = $jpeg->getExif())) { // Create and add empty Exif data to the image (this throws away any old Exif data in the image). $exif = new PelExif(); $jpeg->setExif($exif); } if (!($tiff = $exif->getTiff())) { // Create and add TIFF data to the Exif data (Exif data is actually stored in a TIFF format). $tiff = new PelTiff(); $exif->setTiff($tiff); } if (!($ifd0 = $tiff->getIfd())) { // Create first Image File Directory and associate it with the TIFF data. $ifd0 = new PelIfd(PelIfd::IFD0); $tiff->setIfd($ifd0); } if (!($exif_ifd = $ifd0->getSubIfd(PelIfd::EXIF))) { // Create exif Image File Directory and associate it with the TIFF data. $exif_ifd = new PelIfd(PelIfd::EXIF); $ifd0->addSubIfd($exif_ifd); } if (!($ifd1 = $ifd0->getNextIfd())) { // thumbnail does not exist $ifd1 = new PelIfd(1); $ifd0->setNextIfd($ifd1); //$original = ImageCreateFromString($jpeg->getBytes()); # create image resource of original //$thumb = makeThumb($original); $thumb = $this->_makeThumb($this->getImage()); // start writing output to buffer ob_start(); // outputs thumb resource contents to buffer ImageJpeg($thumb); // create PelDataWindow from buffer thumb contents (and end output to buffer) $window = new PelDataWindow(ob_get_clean()); if ($window) { $ifd1->setThumbnail($window); # set window data as thumbnail in ifd1 } //imagedestroy($original); imagedestroy($thumb); } $exifdata = array(PelTag::IMAGE_DESCRIPTION => $this->getDescription(), PelTag::COPYRIGHT => "webmorph.org: " . $_SESSION['user_id'] . ': IMG_ID: ', PelTag::USER_COMMENT => $this->_embeddedTem); foreach ($exifdata as $PelTag => $val) { if ($PelTag == PelTag::USER_COMMENT) { if (!($entry = $exif_ifd->getEntry($PelTag))) { $exif_ifd->addEntry(new PelEntryUserComment($val)); } else { $entry->setValue($val); } } else { if (!($entry = $ifd0->getEntry($PelTag))) { $ifd0->addEntry(new PelEntryAscii($PelTag, $val)); } else { $entry->setValue($val); } } } $jpeg->saveFile($filename); return true; } catch (Exception $e) { // Handle exception echo $e; } }
/** * This method calculates the image and delivers it to the client. * * @param $folder * @param $file * @param $width * @param $height * @param $mode */ public function resize($folder, $file, $width = -1, $height = -1, $mode = 'nocrop') { $jpeg_orientation_translation = array(1 => 0, 2 => 0, 3 => 180, 4 => 0, 5 => 0, 6 => -90, 7 => 0, 8 => 90); /** * @var JApplicationSite $app */ $app = JFactory::getApplication(); $params = $app->getParams(); if (strcmp($mode, 'full') == 0) { $mode = 'nocrop'; $width = COM_EVENTGALLERY_IMAGE_ORIGINAL_MAX_WIDTH; $height = COM_EVENTGALLERY_IMAGE_ORIGINAL_MAX_WIDTH; } if ($height > $width) { $width = $height; } $sizeSet = new EventgalleryHelpersSizeset(); $saveAsSize = $sizeSet->getMatchingSize($width); $file = STR_REPLACE("\\.\\.", "", $file); $folder = STR_REPLACE("\\.\\.", "", $folder); $width = STR_REPLACE("\\.\\.", "", $width); $mode = STR_REPLACE("\\.\\.", "", $mode); $file = STR_REPLACE("/", "", $file); $folder = STR_REPLACE("/", "", $folder); $width = STR_REPLACE("/", "", $width); $mode = STR_REPLACE("/", "", $mode); $file = STR_REPLACE("\\", "", $file); $folder = STR_REPLACE("\\", "", $folder); $width = STR_REPLACE("\\", "", $width); $mode = STR_REPLACE("\\", "", $mode); $basedir = COM_EVENTGALLERY_IMAGE_FOLDER_PATH; $sourcedir = $basedir . $folder; $cachebasedir = COM_EVENTGALLERY_IMAGE_CACHE_PATH; $cachedir = $cachebasedir . $folder; $cachedir_thumbs = $cachebasedir . $folder; if (!is_dir(JPATH_CACHE)) { //mkdir($cachebasedir, 0777); mkdir(JPATH_CACHE); } if (!is_dir($cachebasedir)) { //mkdir($cachebasedir, 0777); mkdir($cachebasedir); } if (!is_dir($cachedir)) { //mkdir($cachedir, 0777); mkdir($cachedir); } if (!is_dir($cachedir_thumbs)) { //mkdir($cachedir_thumbs, 0777); mkdir($cachedir_thumbs); } $image_file = $sourcedir . DIRECTORY_SEPARATOR . $file; $image_thumb_file = $cachedir_thumbs . DIRECTORY_SEPARATOR . $mode . $saveAsSize . $file; //$last_modified = gmdate('D, d M Y H:i:s T', filemtime ($image_file)); $last_modified = gmdate('D, d M Y H:i:s T', mktime(0, 0, 0, 1, 1, 2100)); #echo "<br>".$image_thumb_file."<br>"; $debug = false; if ($debug || !file_exists($image_thumb_file)) { $ext = pathinfo($image_file, PATHINFO_EXTENSION); $input_jpeg = null; $exif = null; if (strtolower($ext) == "gif") { if (!($im_original = imagecreatefromgif($image_file))) { echo "Error opening {$image_file}!"; exit; } } else { if (strtolower($ext) == "jpg" || strtolower($ext) == "jpeg") { // try to use PEL first. If things fail, use the php internal method to get the JPEG try { $input_jpeg = new PelJpeg($image_file); /* Retrieve the original Exif data in $jpeg (if any). */ $exif = $input_jpeg->getExif(); /* The input image is already loaded, so we can reuse the bytes stored * in $input_jpeg when creating the Image resource. */ if (!($im_original = ImageCreateFromString($input_jpeg->getBytes()))) { echo "Error opening {$image_file}!"; exit; } } catch (Exception $e) { if (!($im_original = imagecreatefromjpeg($image_file))) { echo "Error opening {$image_file}!"; exit; } } } else { if (strtolower($ext) == "png") { if (!($im_original = imagecreatefrompng($image_file))) { echo "Error opening {$image_file}!"; exit; } } else { die("{$ext} not supported"); } } } if ($params->get('use_autorotate', 1) == 1 && $exif != NULL) { $tiff = $exif->getTiff(); $ifd0 = $tiff->getIfd(); $orientation = $ifd0->getEntry(PelTag::ORIENTATION); if ($orientation != null) { $im_original = imagerotate($im_original, $jpeg_orientation_translation[$orientation->getValue()], 0); $orientation->setValue(1); } } $orig_width = imagesx($im_original); $orig_height = imagesy($im_original); $orig_ratio = imagesx($im_original) / imagesy($im_original); $sizeCalc = new EventgalleryHelpersSizecalculator($orig_width, $orig_height, $width, strcmp('crop', $mode) == 0); $height = $sizeCalc->getHeight(); $width = $sizeCalc->getWidth(); //print_r($sizeCalc); // create canvas/border image //adjust height to not enlarge images if ($width > $orig_width) { $width = $orig_width; } if ($height > $orig_height) { $height = $orig_height; } if (strcmp('crop', $mode) != 0) { $canvasWidth = $width; $canvasHeight = ceil($width / $orig_ratio); if ($canvasHeight > $height) { $canvasHeight = $height; $canvasWidth = ceil($height * $orig_ratio); } $width = $canvasWidth; $height = $canvasHeight; } else { $height = $width; } $isOriginalSize = false; if ($height == $orig_height && $width == $orig_width) { $isOriginalSize = true; } /** * Do not recalculate the image if we don't need to resize it. */ if ($isOriginalSize && $params->get('use_sharpening_for_originalsize', 1) == 0) { $im_output = $im_original; } else { $im_output = imagecreatetruecolor($width, $height); $resize_faktor = $orig_height / $height; $new_height = $height; $new_width = $orig_width / $resize_faktor; if ($new_width < $width) { $resize_faktor = $orig_width / $width; $new_width = $width; $new_height = $orig_height / $resize_faktor; } imagecopyresampled($im_output, $im_original, $width / 2 - $new_width / 2, $height / 2 - $new_height / 2, 0, 0, $new_width, $new_height, $orig_width, $orig_height); $use_sharpening = $params->get('use_sharpening', 1); if ($use_sharpening == 1) { // configure the sharpening $stringSharpenMatrix = $params->get('image_sharpenMatrix', '[[-1,-1,-1],[-1,16,-1],[-1,-1,-1]]'); $sharpenMatrix = json_decode($stringSharpenMatrix); if (null == $sharpenMatrix || count($sharpenMatrix) != 3) { $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); } $divisor = array_sum(array_map('array_sum', $sharpenMatrix)); $offset = 0; if (function_exists('imageconvolution')) { if (version_compare(phpversion(), '5.5.9', '=')) { $this->imageconvolution($im_output, $sharpenMatrix, $divisor, $offset); } else { imageconvolution($im_output, $sharpenMatrix, $divisor, $offset); } } } } /** * @var EventgalleryLibraryManagerFolder $folderMgr * @var EventgalleryLibraryFolder $folder */ $folderMgr = EventgalleryLibraryManagerFolder::getInstance(); $folder = $folderMgr->getFolder($folder); $watermark = $folder->getWatermark(); // load default watermark if (null == $watermark || !$watermark->isPublished()) { /** * @var EventgalleryLibraryManagerWatermark $watermarkMgr * @var EventgalleryLibraryWatermark $watermark */ $watermarkMgr = EventgalleryLibraryManagerWatermark::getInstance(); $watermark = $watermarkMgr->getDefaultWatermark(); } if (null != $watermark && $watermark->isPublished()) { $watermark->addWatermark($im_output); } $image_quality = $params->get('image_quality', 85); if ($input_jpeg != null) { Pel::setJPEGQuality($image_quality); /* We want the raw JPEG data from $scaled. Luckily, one can create a * PelJpeg object from an image resource directly: */ $output_jpeg = new PelJpeg($im_output); /* If no Exif data was present, then $exif is null. */ if ($exif != null) { $output_jpeg->setExif($exif); } /* We can now save the scaled image. */ $writeSuccess = true; $output_jpeg->saveFile($image_thumb_file); } else { $writeSuccess = imagejpeg($im_output, $image_thumb_file, $image_quality); if (!$writeSuccess) { die("Unable to write to file {$image_thumb_file}"); } } if (!$writeSuccess) { die("Unable to write to file {$image_thumb_file}"); } $time = time() + 315360000; touch($image_thumb_file, $time); // add the ICC profile try { $o = new JPEG_ICC(); $o->LoadFromJPEG($image_file); $o->SaveToJPEG($image_thumb_file); } catch (Exception $e) { } } $mime = ($mime = getimagesize($image_thumb_file)) ? $mime['mime'] : $mime; $size = filesize($image_thumb_file); $fp = fopen($image_thumb_file, "rb"); if (!($mime && $size && $fp)) { // Error. return; } if (!$debug) { header("Content-Type: " . $mime); header("Content-Length: " . $size); header("Last-Modified: {$last_modified}"); } fpassthru($fp); die; //$app->close(); }
println(' input the input filename, a JPEG image.'); println(' output filename for saving the changed image.'); println(' scale scale factor, say 0.5 to resize to half the ' . 'original size.'); exit(1); } /* The input file is now loaded into a PelJpeg object. */ println('Reading file "%s".', $input); $input_jpeg = new PelJpeg($input); /* The input image is already loaded, so we can reuse the bytes stored * in $input_jpeg when creating the Image resource. */ $original = ImageCreateFromString($input_jpeg->getBytes()); $original_w = ImagesX($original); $original_h = ImagesY($original); $scaled_w = $original_w * $scale; $scaled_h = $original_h * $scale; /* Now create the scaled image. */ $scaled = ImageCreateTrueColor($scaled_w, $scaled_h); ImageCopyResampled($scaled, $original, 0, 0, 0, 0, $scaled_w, $scaled_h, $original_w, $original_h); /* We want the raw JPEG data from $scaled. Luckily, one can create a * PelJpeg object from an image resource directly: */ $output_jpeg = new PelJpeg($scaled); /* Retrieve the original Exif data in $jpeg (if any). */ $exif = $input_jpeg->getExif(); /* If no Exif data was present, then $exif is null. */ if ($exif != null) { $output_jpeg->setExif($exif); } /* We can now save the scaled image. */ println('Writing file "%s".', $output); $output_jpeg->saveFile($output);
function addExif($filename, $exifdata) { try { $jpeg = new PelJpeg($filename); if (!($exif = $jpeg->getExif())) { // Create and add empty Exif data to the image (this throws away any old Exif data in the image). $exif = new PelExif(); $jpeg->setExif($exif); } if (!($tiff = $exif->getTiff())) { // Create and add TIFF data to the Exif data (Exif data is actually stored in a TIFF format). $tiff = new PelTiff(); $exif->setTiff($tiff); } if (!($ifd0 = $tiff->getIfd())) { // Create first Image File Directory and associate it with the TIFF data. $ifd0 = new PelIfd(PelIfd::IFD0); $tiff->setIfd($ifd0); } if (!($exif_ifd = $ifd0->getSubIfd(PelIfd::EXIF))) { // Create first Image File Directory and associate it with the TIFF data. $exif_ifd = new PelIfd(PelIfd::EXIF); $ifd0->addSubIfd($exif_ifd); } if (!($ifd1 = $ifd0->getNextIfd())) { // thumbnail does not exist $ifd1 = new PelIfd(1); $ifd0->setNextIfd($ifd1); $original = ImageCreateFromString($jpeg->getBytes()); # create image resource of original $thumb = makeThumb($original); // start writing output to buffer ob_start(); // outputs thumb resource contents to buffer ImageJpeg($thumb); // create PelDataWindow from buffer thumb contents (and end output to buffer) $window = new PelDataWindow(ob_get_clean()); if ($window) { $ifd1->setThumbnail($window); # set window data as thumbnail in ifd1 } imagedestroy($original); imagedestroy($thumb); } foreach ($exifdata as $PelTag => $val) { if ($PelTag == PelTag::USER_COMMENT) { if (!($entry = $exif_ifd->getEntry($PelTag))) { $exif_ifd->addEntry(new PelEntryUserComment($val)); } else { $entry->setValue($val); } } else { if (!($entry = $ifd0->getEntry($PelTag))) { $ifd0->addEntry(new PelEntryAscii($PelTag, $val)); } else { $entry->setValue($val); } } } $jpeg->saveFile($filename); return true; } catch (Exception $e) { // Handle exception echo $e; } }
function saveEdit() { // check for image editor access if (!$this->checkAccess('image_editor', 1)) { JError::raiseError(403, 'RESTRICTED ACCESS'); } $editor = $this->getImageEditor(); $browser = $this->getBrowser(); $args = func_get_args(); // file src $file = array_shift($args); // check file WFUtility::checkPath($file); // file name $name = array_shift($args); // check name WFUtility::checkPath($name); // check for extension in destination name if (preg_match('#\\.(php|php(3|4|5)|phtml|pl|py|jsp|asp|htm|html|shtml|sh|cgi)\\b#i', $name)) { JError::raiseError(403, 'INVALID FILE NAME'); } // edit data $props = array_shift($args); // exif data $exif = null; $data = JRequest::getVar('data', '', 'POST', 'STRING', JREQUEST_ALLOWRAW); if (preg_match('#data:image\\/(jpeg|jpg|png|bmp);base64#', $data)) { // replace spaces $data = str_replace(' ', '+', $data); // remove header $data = substr($data, strpos($data, ",") + 1); // decode data $data = base64_decode($data); $src = WFUtility::makePath(JPATH_SITE, $file); $dest = dirname($src) . DS . basename($name); // get exif data from orignal file if (preg_match('#\\.jp(eg|g)$#i', basename($file)) && basename($file) == basename($dest)) { // load exif classes require_once dirname(__FILE__) . DS . 'pel' . DS . 'PelJpeg.php'; $jpeg = new PelJpeg($src); $exif = $jpeg->getExif(); } if (!JFile::write($dest, $data)) { $browser->setResult(WFText::_('WF_IMGMANAGER_EXT_ERROR'), 'error'); } else { $browser->setResult(basename($dest), 'files'); if ($exif && basename($file) == basename($dest)) { $pel = new PelDataWindow($data); if (PelJpeg::isValid($pel)) { $jpeg = new PelJpeg(); $jpeg->load($pel); /*$dim = @getimagesize($dest); if ($dim) { $tiff = $exif->getTiff(); $ifd0 = $tiff->getIfd(); $width = $ifd0->getEntry(PelTag::IMAGE_WIDTH); $height = $ifd0->getEntry(PelTag::IMAGE_LENGTH); $width->setValue($dim[0]); $height->setValue($dim[1]); }*/ $jpeg->setExif($exif); $jpeg->saveFile($dest); } } } } else { $browser->setResult(WFText::_('WF_IMGMANAGER_EXT_ERROR'), 'error'); } return $browser->getResult(); }
/** * @param $from_file * @param $to_file */ function copy_exif($from_file, $to_file) { $size = @getimagesize($to_file); if ($size) { require_once dirname(__FILE__) . '/pel/autoload.php'; try { Pel::setJPEGQuality(100); /* * We want the raw JPEG data from $scaled. Luckily, one can create a * PelJpeg object from an image resource directly: */ $input_jpeg = new PelJpeg($from_file); /* Retrieve the original Exif data in $jpeg (if any). */ $input_exif = $input_jpeg->getExif(); /* If no Exif data was present, then $input_exif is null. */ if ($input_exif != null) { $input_tiff = $input_exif->getTiff(); if ($input_tiff == null) { return; } $input_ifd0 = $input_tiff->getIfd(); if ($input_ifd0 == null) { return; } $input_exif_ifd = $input_ifd0->getSubIfd(PelIfd::EXIF); $input_inter_ifd = $input_ifd0->getSubIfd(PelIfd::INTEROPERABILITY); $orientation = $input_ifd0->getEntry(PelTag::ORIENTATION); if ($orientation != null) { $orientation->setValue(1); } if (!empty($input_ifd0)) { /*$x_resolution = $input_ifd0->getEntry( PelTag::X_RESOLUTION ); $y_resolution = $input_ifd0->getEntry( PelTag::Y_RESOLUTION ); if ( $x_resolution != null && $y_resolution != null ) { //$x_res = $x_resolution->getValue(); //$y_res = $y_resolution->getValue(); $x_resolution->setValue( $y_res ); $y_resolution->setValue( $x_res ); }*/ $image_width = $input_ifd0->getEntry(PelTag::IMAGE_WIDTH); $image_length = $input_ifd0->getEntry(PelTag::IMAGE_LENGTH); if ($image_width != null && $image_length != null) { $image_width->setValue($size[0]); $image_length->setValue($size[1]); } } if (!empty($input_exif_ifd)) { $x_dimention = $input_exif_ifd->getEntry(PelTag::PIXEL_X_DIMENSION); $y_dimention = $input_exif_ifd->getEntry(PelTag::PIXEL_Y_DIMENSION); if ($x_dimention != null && $y_dimention != null) { $x_dimention->setValue($size[0]); $y_dimention->setValue($size[1]); } } if (!empty($input_inter_ifd)) { $rel_image_width = $input_inter_ifd->getEntry(PelTag::RELATED_IMAGE_WIDTH); $rel_image_length = $input_inter_ifd->getEntry(PelTag::RELATED_IMAGE_LENGTH); if ($rel_image_width != null && $rel_image_length != null) { $rel_image_width->setValue($size[0]); $rel_image_length->setValue($size[1]); } } $output_jpeg = new PelJpeg($to_file); $output_jpeg->setExif($input_exif); /* We can now save the image with input_exif. */ $output_jpeg->saveFile($to_file); } } catch (PelException $e) { } } }
private function createNewImageGD($sourcePathBase, $targetPath, $newWidth, $newHeight, $sourceWidth, $sourceHeight) { $status = false; if (!$this->sourceGdImg) { $this->sourceGdImg = imagecreatefromjpeg($sourcePathBase); if (class_exists('PelJpeg')) { $inputJpg = new PelJpeg($sourcePathBase); $this->exif = $inputJpg->getExif(); } } ini_set('memory_limit', '512M'); $tmpImg = imagecreatetruecolor($newWidth, $newHeight); //imagecopyresampled($tmpImg,$sourceImg,0,0,0,0,$newWidth,$newHeight,$sourceWidth,$sourceHeight); imagecopyresized($tmpImg, $this->sourceGdImg, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight); if ($this->jpgCompression) { $status = imagejpeg($tmpImg, $targetPath, $this->jpgCompression); if ($this->exif && class_exists('PelJpeg')) { $outputJpg = new PelJpeg($targetPath); $outputJpg->setExif($this->exif); $outputJpg->saveFile($targetPath); } } else { if ($this->exif && class_exists('PelJpeg')) { $outputJpg = new PelJpeg($tmpImg); $outputJpg->setExif($this->exif); $status = $outputJpg->saveFile($targetPath); } else { $status = imagejpeg($tmpImg, $targetPath); } } if (!$status) { $this->logOrEcho("\tERROR: Unable to resize and write file: " . $targetPath . "\n"); } imagedestroy($tmpImg); return $status; }