Or other image file. Can this be done in a (fairly) simple way using PHP?
I'm coding a website that will allow users to upload photos, but I know JPEGs are notorious for their metadata and ideally I'd like to strip all images uploaded of metadata, either by removal or replacement with junk text.
If you're just looking to strip most of the exif data quickly and easily without using a library to specifically write it, you can 'resave' the image using gd:
$file = 'myjpg.jpg';
$im = imagecreatefromjpeg($file);
imagejpeg($im, 'myjpg2.jpg');
Maybe not the best/prettiest solution, but it accomplishes what you want without adding additional libraries.
Take a look at this extension for PHP: http://lsolesen.github.io/pel/
i use getID3 library for this. it makes it quite easy
http://getid3.sourceforge.net/
it works with al lot of file extentions
for a demo click here:
demo
Related
I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compresses a JPEG that is already on the server (I don't want to upload a new one), and replaces the old one.
Does PHP have a built in function for this? If not, how do I read the JPEG from the folder into the script?
Thanks.
you're not telling if you're using GD, so i assume this.
$img = imagecreatefromjpeg("myimage.jpg"); // load the image-to-be-saved
// 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)
imagejpeg($img,"myimage_new.jpg",50);
unlink("myimage.jpg"); // remove the old image
I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:
$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest);
$img->clean();
You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.
For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html
The solution provided by vlzvl works well. However, using this solution, you can also overwrite an image by changing the order of the code.
$image = imagecreatefromjpeg("image.jpg");
unlink("image.jpg");
imagejpeg($image,"image.jpg",50);
This allows you to compress a pre-existing image and store it in the same location with the same filename.
On our site, we get a large amount of photos uploaded from various sources.
In order to keep the file sizes down, we strip all exif data from the source using mogrify:
mogrify -strip image.jpg
What we'd like to be able to do is to insert some basic exif data (Copyright Initrode, etc) back onto this new "clean" image, but I can't seem to find anything in the docs that would achieve this.
Has anybody any experience of doing this?
If it can't be done through imagemagick, a PHP-based solution would be the next best thing!
Thanks.
You can save a large amount of space, especially if you have a large number of images..
Add the following to text.txt (format of the IPTC tags taken from here):
2#110#Credit="My Company"
2#05#Object Name="THE_OBJECT_NAME"
2#55#Date Created="2011-02-03 12:45"
2#80#By-line="BY-LINE?"
2#110#Credit="The CREDIT"
2#115#Source="SOURCE"
2#116#Copyright Notice="THE COPYRIGHT"
2#118#Contact="THE CONTACT"
2#120#Caption="AKA Title"
Strip all existing exif data from the image
mogrify -strip image.jpg
Add the credit to your image
mogrify -profile 8BIMTEXT:text.txt image.jpg
Exiftool looks like it would be an exact match for you.
I haven't tried it but I'm now tempted to go and fix all my honeymoon photos which are marked 01/01/2074 because I forgot to reset the date after the batteries died.
Here's a PHP Exif Library that should do what you need.
The PHP Exif Library (PEL) lets you
fully manipulate Exif (Exchangeable
Image File Format) data. This is the
data that digital cameras place in
their images, such as the date and
time, shutter speed, ISO value and so
on.
Using PEL, one can fully modify the
Exif data, meaning that it can be both
read and written. Completely new Exif
data can also be added to images. PEL
is written completely in PHP and
depends on nothing except a standard
installation of PHP, version 5. PEL is
hosted on SourceForge.
on linux there is a program called jhead. It can add a minimal exif header with the command:
jhead -mkexif img.jpg
I doubt you will gain lot of space by removing Exif information...
Anyway, I can be wrong, but Exif metadata belongs more to store technical (and contextual) information. For stuff like copyright, you should use IPTC instead.
That's something you can do, apparently, with ImageMagick: Write IPTC Data to Jpeg with ImageMagick.
You can do this directly in PHP using the PEL library. You would do this by simply overwriting the existing EXIF-headers,
// Load image data
$data = new PelDataWindow(file_get_contents('IMAGE PATH'));
// Prepare image data
$jpeg = $file = new PelJpeg();
$jpeg->load($data);
// Create new EXIF-headers, overwriting any existing ones (when writing to disk)
$exif = new PelExif();
$jpeg->setExif($exif);
$tiff = new PelTiff();
$exif->setTiff($tiff);
// Create Ifd-data that will hold EXIF-tags
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
// Create EXIF-data for copyright
$make = new PelEntryAscii(PelTag::COPYRIGHT, '2008-2017 Conroy');
$ifd0->addEntry($make);
// Add more EXIF-data...
// Save to disk
$file->saveFile('IMAGE.jpg');
You can find a complete list of all supported EXIF-data (PelTag) in the PEL docs.
I'm working on a project where I upload an image (jpg) and manipulate it using the PHP GD library.
I know that I can use GD functions to edit an image resource (created from imagecreatefromjpeg()) but I was wondering if there was a way I could use the file uploaded in the $_FILES array directly with the GD library. One solution I thought of was saving the uploaded file, pushing it into imagecreatefromjpeg, then deleting it afterwards.
This seems cluinky though, is there a more efficient solution?
I'm still a bit new to PHP so I'm not sure as to how files are stored in the $_FILES array. I hope I'm making sense here. Thanks.
You can simply do this:
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
// do gd operations on $img
imagejpeg($img, '/path/to/target');
You'll have to use imagecreatefrom in some form or another, and you can use it directly on the uploaded file. Then just save the result of your manipulations using imagejpeg. The uploaded file in tmp_name will we thrown away automatically.
Having said that, you should save the original somewhere. It's always good to have it around for later use.
is that possible to show whole php page as an image file like png or jpg ?
for example,
<img src="details.php?id=44">
just like a screenshot of the page.
Thx
<?
// details.php file
// {
// do stuff to create your image and store in $my_image
// }
header("Content-type: image/jpg");
echo $my_image;
?>
1) output the mime type in the header.
2) echo your binary data
easy!
Not in the way you're thinking. You can use the PHP ImageMagick library or the PHP GD Library to generate an image dynamically and output it, but in order to get screenshots of a page you'll really need an outside service.
it depends on what you want to show. if its html/js/css, it varies across browsers.
If its unicode text to be displayed as images, you can use GD library for the purpose
I think you are talking about taking screenshots of a url or webpage. You can you freely available like:
http://www.shrinktheweb.com/
http://picoshot.com/
There are many such freely available services. If you want to host such service on your own server. You will need to create a software which can open your browser and take screenshot of the url. Ther are some softwares available as well. I hope it helps.
I'm using PHP to copy JPGs from a remote server to my own server. Is it best to simply use the copy() function, or are the jpeg-specific functions better? For example:
$copy = copy($remote_url, $dest_file);
-OR-
$img = imagecreatefromjpeg($remote_url);
$copy = imagejpeg($img, $dest_file);
imagedestroy($img);
What would the best option be in terms of speed and memory load? Also, would there be any difference in the resulting image quality? I should add that this script is required to copy a large number of photos (typically hundreds, but sometimes it may be a couple thousand).
Thanks, Brian
if all you want is a copy, copy() is better.
using the gd library functions (imagecreatefromjpeg/imagejpeg) will end up re-compressing the image (probably, maybe it's smart enough not to, but probably). If you wanted to convert the images to .png or something, then you'd want to use gd (or ImageMagick)