I'm new to php, but i'm fairly certain this is possible to do. I have a bunch of images on my server, and i'd like to give them all a thick black border. I know i could use CSS, but i'd rather the border was real. My images are all the same size, so it's nothing more than centering the server image onto this black box image, then merging them together and re-saving the server image.
I could technically do this in photoshop too, but there's a ton of images...
Bonus
If i could shrink the image after i'm done, that'd be nice too. They are a bit larger than i need.
Take a look at imagemagicks, see some examples here http://www.imagemagick.org/script/examples.php
You can call it via exec() from PHP
e.g. aligning two images next to each other, adding a border around each of them:
$cmd = 'montage image1.jpg image2.jpg -tile x1 -border 5 -geometry +5+5 result.jpg';
exec($cmd);
The GD2 library also gives generally good results if ImageMagick isn't installed on your server.
See some tutorials here: http://www.roseindia.net/tutorial/php/phpgd/
Some say that ImageMagick gives better results in many cases, but GD2 may suit what you need just fine and is fairly easy to use. Hopefully, it matches the use case you're describing.
Related
I'm trying to compare shapes in php. I have a database with a lot of images, in those images there is a shape ( mostly in the center of the image ). Now I want to compare those images to a Shape.
The input is a shape (png) that is exact the same size then the images in the database.
I made a function that takes the color of the shape and turns it in black ( and the background in white)
The input is like this:
The original is like this:
I can compare every pixel in the image and track how many black pixels are the same, but this will take a lot of CPU and I think it will not work every time.
I also can use an another language like python, but I really wanne fix this in PHP. Does anyone has an idea to do this in a practical way?
The anwser in the question "Compare 2 images in php" is different then I expect. I need to compare a part of an image ( in the example the background in white, but it can be there is some noise in the background )
This seems to be doable with the trimImage() function in ImageMagick (-trim flag on the command line). Considering you were already able to convert the image into a black foreground and white background, this function should always do what you want. At this point just compare images as stated in the question you linked.
This is the result of using trim on the command line on the image with padding:
I assume that the function that you made is similar to the algorithm in findimagedupes. If not, you should borrow that idea.
I'm building a web application that needs to be able to crop an image based on the location of a 4-sided shape with a white border (and no fill) contained in the image. All images uploaded by users are going to be dark so they shouldn't interfere with the algorithm to find the bounding box. This part of the application should be able to handle all types of quadrilaterals and will stretch whatever is contained within the white-bordered shape to fit a square.
My lazy rendition in Photoshop: http://i.stack.imgur.com/xJjoL.png
I'm using Laravel to build this application (LAMP stack). I'm looking at libraries such as ImageMagick and GD but I'm still new to programatically editing images. Are there specific functions in IM or GD that I could call to help me build this part of the application? Are there better PHP libraries that have the ability to do this? I'm not asking for code necessarily; I just want to know what tools I should start with before I build this.
Imagemagick can be used for this type of application if I understand what you're asking. To change the perspective of an image, look into:
http://www.imagemagick.org/Usage/distorts/#perspective
To crop images after distorting, look into:
http://www.imagemagick.org/Usage/crop/#crop
It would be easy to make a bash loop to accomplish these two tasks. Something like this:
## first distort the images
for i in *.jpg ##or whatever format you have (.tiff, pdf, etc..)
do
convert $i -distort Perspective out_file.png ## where you give coordinates into the perspective method
done
## now to crop the distorted images
for i in *.png
do
convert $i -crop out_file.jpg ## where you give the coordinates of the new rectangle to the crop method
done
The code above is just a STARTING point and would need to be populated according to the links I mentioned above. Happy scripting!
You may want to look into OpenCV for recognizing the rectagle. GD and image magic are good at editing images using filters, but they don't do a lot (or any?) of object detection.
See: https://github.com/ProGM/OpenCV-for-PHP/wiki/Installation-guide
I am sure I could somehow figure this out but it's just taking me way too long since I am not a PHP guy. Hopefully someone can set up the script for me in minutes...
So here's what I need to do:
I transmit 2 pictures from a smartphone to a webserver. These 2 images need to be merged (watermarked). They differ a little:
picture is a 2 megapixel jpg (holding a photo)
picture will be a png of 480x800 pixels with a transparent background (holding a simple finger painting)
Now I need to merge these images. The 2nd one (png) needs to be scaled to the 1st one's (jpeg) resolution.
Please note 2 things:
I can only use the GD library that is installed on the server. Imagick or alike is not available
I am well aware of the fact that the 2nd image's quality won't be brilliant. That's okay.
So could anyone help me out on this? Like I said, I've been messing around with a couple of GD functions but progress is far too slow. I guess I found the required functions with imagecopy and watermark. But I don't find the right way to put 'em together.
Thanks in advance,
steff
Thanks a million
A few months ago i posted a function that does that, since the code is quite big i will just link my post, check it out here. Make sure you don't do this onfly, it will overload your server, save the watermarked images or at least cache them.
If you also need to resize the original or watermark, i also posted a function to do this here.
I have a large SVG file (approx. 60 MB, 10000x10000 pixels but with the potential to get much larger), and I'm wanting to create, say, many tiled 256x256 PNG images from it (in that example there would be 1600 images; round(10000/256)^2).
Does anyone have any idea of how to do this on a web server (running PHP amongst other things)? I thought about rsvg, but it doesn't seem to have any functionality to modify the bounding box (and I'd rather avoid doing it manually for each section). ImageMagick might be able to do it, but I've not been having much luck with getting it to work. Using rsvg to create a large PNG and then using a tool dedicated to tiling very large images might work, but I've not had any luck finding such a thing! Speed isn't really an issue, although it is desirable, so if the worst comes to the worst, I might look into modifying the SVG's bounding box per section. I could see the generation taking forever, though!
Anyone know of any methods to do this?
Edit 2016-03-02:
I recently came back to needing an answer for this question again, and Inkscape appears to be the only tool which can render SVGs for a given area at given sizes (svgexport almost meets these requirements, but it doesn't let you change the aspect ratio).
My aim was to tile an SVG into 256x256 tiles, and I've now successfully made a script which can tile an arbitrarily large SVG by doing repeated renderings in inkscape of about 16,000 x 16,000 and tiling the resulting images. I've successfully rendered SVGs where the dimensions are over 500,000 x 500,000 pixels—no problems with memory usage (it just takes a long time!)
inkscape has a command line mode to export pngs, taking an optional argument to choose which area to export
inkscape vector.svg --export-png=raster.png --export-area=0:0:100:100
I'd look at Apache Batik. In particular, their SVG Rasterizer looks like just what you need.
I've never used it for giant SVG files, though, so I don't know if it's optimized for that case or not.
Check out this question i posted earlier and got working.
If the image is only 10000x10000 the script i have in the question works best.
If however you want to use much bigger images check out the script in my anser.
ImageMagick crop huge image
PanoJS seems to do what you're asking about. You need to convert the SVG to a large PNG first though (e.g. using inkscape on the command line), and then use PanoJS's tilemaker to make the tiles. It is a very memory intensive beast, but if you can get it to run successfully, you can then use the PanoJS Javascript code to point to your webserver. XKCD used it for a large image describing money.
You might want to edit the source properties of your SVG (a copy), to render certain areas only. Use the "width" and "height" properties to match your desired tile size (256) and the "viewBox" to the desired tile area (for example 'viewBox="512 256 768 512"' for the 3rd tile in the second row).
You could do something like this in a loop:
$sed = "sed 's/width=\"10000\"/width=\"256\"' ".$sourcefile;
$sed .= " | sed 's/height=\"10000\"/height=\"256\"'";
$sed .= " | sed 's/viewBox=\"0 0 10000 10000\"/viewBox=\"0 0 256 256\"'";
exec($sed." > ".$tmpfile);
exec('rsvg '.$tmpfile.' > '.$tilefile);
I don't know how this behaves on very large files though.
I have a whole bunch of images of illustrations that I would like to crop to a smaller preview size.
The problem is that I want to crop them to show an "interesting" part of the illustration (ie avoid areas of whitespace).
The images typically have a flat color or a subtle gradient for the background. They are mostly vector style artwork with fairly distinct shapes.
Here are some examples: link ;-)
I've been thinking about using some sort of image feature detection algorithm with a sliding window to find the area with the greatest number of features.
I'm implementing this in PHP, but I don't mind implementing it myself if there isn't a library or extension available.
Ideas?
ImageMagick has a trim operation. It's available as a library but I don't know how hard it is to use from PHP. There are some PHP interfaces.
OK, so here's what I would've done, after looking at the examples:
Sum all rows and all columns of each image. You'll get two arrays, both looking like this:
/-----\ /--\
_/ -- |
___- \_________
By looking at these arrays for a few images, find a suitable threshold (probably something just above zero). Then the leftmost and the rightmost crossing of this threshold is where you have to crop. I hope I've managed to make it clear enough, if not -- ask!
Here's a fairly simple approach using an edge-detection filter, and then cropping around the center-of-edginess of the image to generate a thumbnail. It works pretty well on most images, but not if there are more than one subject. I'm open to suggestions on other ways of identifying the "interesting" points in a source image.
Well, you might want to consider just using an edge detection algorithm. Pick the area with the largest number of edges. Give higher weight to edges that are not blurry (as they may be from the background).
ImageMagick for PHP has automated generation of thumbnails. This SO question has a link to an ImageMagick auto-crop operator, and I'm not sure, but I think this is the PHP interface to it.
From the link:
bool Imagick::trimImage ( float
$fuzz )
Remove edges that are
the background color from the image.
For more general "interestingness", maybe try an inverse of seam carving (to find the highest energy, rather than lowest energy areas).
A CLI program using http://pecl.php.net/package/imagick:
<?php
dl('imagick.so');
$img = new Imagick();
$img->readImage($argv[1]);
# (* 0.0: exact match; * 1.0: crop entire image)
$fuzz = current($img->getQuantumRange()) * 0.25;
$img->trimImage($fuzz);
$img->writeImage($argv[2]);
?>
It should work good enough, as long as the image doesn't have a frame around its border.
Drupal has a project called smartcrop, which has PHP code to find highest entropy and "interesting" areas in images. See the output examples.
You should be able to use the functions in the module and the library in none-Drupal projects too.