Server-side conversion of raster images to vector images - php

I would like to convert images that have been uploaded by the user (in various formats and conditions) to a vector image format such as .eps. I'm primarily working in PHP.
What options exist?

There are a small number of autotracing software projects released under GPU (for example, POTRACE that you could run via system commands. I can't attest to their quality. Tracing almost always requires some element of human supervision to avoid things looking like a mess of broken pottery, but you won't know until you try. Rather than triggering the tracer via PHP, I would use PHP simply to save incoming images to a temporary folder and then, through cronjob (one- or two-per-minute), crank through the holding folder in batches (you could pace it that way and avoid it being used as a way to DoS your site).
I'm thinking of doing something slightly similar (though not graphic related) for an upcoming project, and I'm considering doing all my heavy lifting on a desktop machine, which would fetch all incoming files and process them before FTPing them back to the server. I'm somewhat nervous about having any complex resource-intensive script like this running on a web server.

Definitelly you can do this with the Inkscape
here is the list of formats it supports What formats can Inkscape import/export?
and it can be of course used with the command line or exec() command Can Inkscape be used from the command line?

Imagetracer is a free and open source (Public Domain) library and application which can be used on the server side. Disclaimer: I made these.
You can use ImageTracer.jar from
https://github.com/jankovicsandras/imagetracerjava
like this with PHP:
<?php exec("java -jar ImageTracer.jar input.png outfilename output.svg"); ?>
You can also use the JavaScript version with Node.js on the server side, here's the example code:
https://github.com/jankovicsandras/imagetracerjs/tree/master/nodecli
https://github.com/jankovicsandras/imagetracerjs/blob/master/nodetest/nodetest.js

PHP is not an image editor. It is a hypertext preprocessor.
You have to move to serverfault.com, or even better on some image processing resource, and ask there for some command line utility that can be run from PHP using the system() command.

Related

extract images from PDF with PHP

The thing is that the client wants to upload a pdf with images as a way of batch processing multiple images at once.
I already looked around and out of the box PHP can't read PDF's.
What are my alternatives?
I already know the host has not installed imageMagick or any pdf library and the exec function is disabled. That's basicly leaving me with nothing to work with, I guess?
Does anyone know if there is an online service that can do this, with an api of sorts?
thanks in adv
AFAIK, there is no PHP module to do it. There is a command line tool, pdfimages (part of xpdf). For reference, here's how that works:
pdfimages -j source.pdf image
Which will extract all images from source.pdf as image-000.jpg, image-001.jpg, etc. Note the output format is always Jpeg.
Possible Options
Being a command line tool, you need exec (or system, passthru, any of the command executing functions built into PHP). As your environment doesn't have that, I see four options:
Beg that exec be turned on for you (your hosting provider can limit what you can exec to a single command)
Change the design -- how about a ZIP upload?
Roll your own, using the source code of pdfimages as a model
Let pdfimages do the heavy lifting, by running it on a remote host you do control
Regarding #3, rolling your own, I don't think rolling your own, to solve a very narrow definition of requirements, would be too difficult. I seem to recall that the image boundaries in PDF are well defined: just read in the file to a boundary, cut to the end of the boundary, base64_decode, and write to a file -- repeat. However, that may be too much...
If rolling your own is too complicated, then option #4 is kind of like what Joel Spolsky describes for working with complicated Excel objects (see the numbered list under the bold heading "Let Office do the heavy work for you").
Find a cheap hosting environment (eg Amazon EC2) that let's you exec and curl
Install pdfimages
Write a PHP script that takes a URL to a PDF, curl opens that PDF, writes it to disk, passes it to pdfimages, then returns the URL to the resulting images.
An example exchange could look like this:
GET http://www.cheaphost.com/pdfimages.php?extract=http://www.limitedhost.com/path/to/uploaded.pdf
Content-type: text/html
<html>
<body>
<ul>
<li>http://www.cheaphost.com/pdfimages.php?retrieve=ab9895v/image-000.jpg</li>
<li>http://www.cheaphost.com/pdfimages.php?retrieve=ab9895v/image-001.jpg</li>
</ul>
</body>
</html>
So your single pdfimages.php script (running on the host with the exec functionality) can both extract images, and give you access to the extracted images. When extracting, it reads a PDF you tell it, runs pdfimages on it, and gives you back a list of URL to call to retrieve the extracted images. When retrieving, it just gives you back a straight image.
You would need to deal with cleanup, perhaps the thing to do would be to delete the image after retrieval. You would also need to handle security -- don't know what's in these images, but the content might need to be wrapped in SSL and other precautions taken.
You can use pdfimages and install it this way:
apt install poppler-utils
Then use it this way to get all the images as PNG files:
pdfimages -j mypdf.pdf image -png
Images will be placed in the same folder under image-000.png, image-001.png, etc.
There are many options available, including some to change the output format, more information here.
I hope this helps!

Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?

I need to do the following image manipulations for images uploaded by users on my site:
Resize images (if greater than a certain dimension)
Convert all image formats to jpg's
Add a watermark to the bottom of all images
Do I need to use either the MagickWand or iMagick extensions or can I just get away with running the terminal commands inside PHP's exec function?
Is there a reason why the PHP extensions would be preferred? Which would be faster and better for performance (this site may have lots of users and there would be a ton of image processing at any given time)?
I'd like to make a counterpoint to drew010's answer. In his answer he states:
You would benefit a lot using the PHP extensions instead of using exec
or similar functions. Built in extensions will be faster and use less
memory as you will not have to spawn new processes and read the output
back
For processing images, this is true. Well, I know for a fact that calling on ImageMagick binaries using PHP's exec() function (or similar functions) carries additional overhead above using embedded PHP libraries however I'm not clear on how much overhead there is.
But there's another side of the coin. If you embed the ImageMagick code into PHP via an extension, then every request using PHP takes more memory whether the request processes an image or not. If you're using Apache's mod_php then this becomes even more of an issue because now every request to your server has the ImageMagick libraries in memory even if it's serving an HTML file!
So it really depends. You really want to keep PHP's memory footprint as low as possible. If you're processing a large number of requests which require ImageMagic and you're using FastCGI or php_fpm, then you'll probably see a benefit to using embedded ImageMagick. But if you're only occasionally processing requests using ImageMagick and/or using Apache's mod_php then you may get much better performance calling ImageMagick via exec().
You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.
If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.
I always use PHP GD http://php.net/manual/en/book.image.php
You can accomplish resizing, converting to JPG and watermarking your images. I know your post said you have to use MagickWand or iMagick, but I just wanted to present this option in case it would work for you.

Using Photoshop Actions on Images uploaded by Users

I want to have a user upload a image to my website and have a series of Photoshop actions like adding a layer behind of that image, a layer infront and some dynamic text with effects applied to it like drop shadow and gradient overlay. Is this even possible?
Have a look at Various Image Processing and Generation functions provided by PHP mainly ImageMagick & GD. If that does not prove to be sufficient,
Consider Gimp command line. you can easily invoke it from php using exec() function. GIMP is full featured & can do almost all fancy Photoshop actions you would want. And its free!
Hope it helps
PHP libraries are available to perform some basic graphics manipulation, as mentioned in the other answer. If these aren't adequate for you, it looks as though you can run and script the normal Photoshop program on your server. Photoshop is not available for Linux, so you'd probably need a Windows server. (This wouldn't be doable on shared hosting, you'd probably need a decent VPS at least.)
I haven't done this before, but according to Adobe Photoshop Scripting documentation your language choices are JavaScript and VBScript. Someone could write a PHP library to access this, but I don't know of anything like that.

Conversion script

Hello I am wondering what processes involves converting files online...What programming languages are required?Basically I am wondering how are the files on scribd,issu,slideshare converted...
Thank you..!
There is a media processing library called FFMpeg. It will read, write and convert pretty much any common media format. It can resize, crop, scale, resample, etc all the files it can load, meaning videos can be shrunk in size, or whatever you might want to do!
The great thing is, if you have PHP and FFMpeg installed on a server, you can use PHP's exec() command to convert/modify/save videos.
A little note: beware of using exec() with any commands that are influence by what is sent from the server, like frame sizes, etc - hackers can latch onto them and mess your server up!
To get round the problem of pethetic people (script kiddies, etc), try ffmpeg-php. I don't know how popular this is on web hosts, but I haven't seen it many places in the wild.
James
EDIT
Unfortunately not. FFMpeg is primarily for video/audio conversion. However, there is a program called pdf2swf that should do the trick. The man page for it is here.
The only concern about pdf2swf is whether your web host actually has it installed on their server. If you have a VPN or a dedicated server, that's no problem, but if you're on shared hosting, and have no access to the root filesystem, this is where issues arise - you can't install pdf2swf if you don't have it.

how to scan image on php or javascript?

i dont know its possible or no?
i have a project in php ,
can we scan image on php or javascript or ... via scanner , is any way for that?
Assuming that the scanner is connected to the server where PHP is being executed:
Currently there's some good solutions to scan images through PHP by making use of the Open Source SANE Scanning software:
SCANPHP
A PHP Lightweight Scanning GUI which makes use of the Open Source SANE Scanning software. The PHP GUI can be installed on any Web Server as long as PHP can be run. PHP calls the scanimage command in order to provide the scan. Post scanning, the image is piped "|" to gocr / pnmtojpeg in order to provide the acquired file.
PHPSANE
phpSANE is a web-based frontend for SANE written in HTML/PHP so you can scan with your web-browser. It also supports OCR.
A code example would be:
exec("scanimage --mode Gray --resolution 150 | pnmtojpeg > /tmp/image.jpg");
You can refer to the SANE Project Homepage for further information on options available, installation steps, etc.
It's possible, but only using third-party plug-ins or applets, most of which are not free and limited to a platform (PC / Windows mostly, and some even to Internet Explorer, although there are ActiveX wrappers for other browsers, too.)
Check out the answers to this question. They should give you a good overview about what's possible.
It is impossible to use scanner from the JavaScript on the client unless you have a special browser interface for that. No major browser have built-in support for that -- only via plugins.
Here is some more info:
http://www.ciansoft.com/samples/tcxbrowser.htm
http://www.chestysoft.com/ximage/twainupload.asp

Categories