-4

I want to write a simple web service (I'm not particular about the language implementation) that runs on a FreeBSD web server at nearlyfreespeech.net, takes a URL that points to a JPEG image with some additional parameters like the following:

http://www.myserver.com/script.cgi?url=http://www.destination.com/image.jpg&width=320

and performs the following operations:

  1. How do I parse the URL's parameters? (destination URL plus some additional parameters for processing)
  2. How can I fetch the contents of the image at the URL provided in the parameter?
  3. Based on the additional parameters, perform some processing on the contents (out of scope of this question)
  4. How can I return the processed image to be properly recognized as an image to be displayed in a browser (as opposed to returning text)?

I presume this wouldn't be difficult to do with a scripting language like Perl, but I don't know where to begin for steps 1, 2, and 4.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
  • 1
    So, you've basically listed the *how*. So what do you want exactly? Are you having a specific issue? – netcoder Aug 17 '12 at 19:26

5 Answers5

1

The url you supplied is invalid, but you could encode the querystring such as: http://www.myimageserver.com?url=http%3a%2f%2fwww.flickr.com%2fsomeimage.jpg%2f320%2f200 (here the flickr url is urlencoded, you can try urlencoding online here: http://www.opinionatedgeek.com/DotNet/Tools/UrlEncode/Encode.aspx)

  1. Using php, you can get the parameters of the url. To access "http://www.flickr.com/someimage.jpg/320/200" in php you simply use:

    $url = $_GET['url']; // set $url to the string: "http://www.flickr.com..."

    // ... parsing can be done here

  2. To get the contents of the url:

    $url_content = file_get_contents($url);

  3. Now you can do your processing.

  4. And to return the output, you can use echo:

    echo $processed_content;

Robin Manoli
  • 2,162
  • 2
  • 25
  • 30
1

I've been working on a PHP scraper recently, so I'm reasonably well-acquainted with some of these issues.

I'd suggest the following solutions for those issues:

  1. You can get the parameters easily enough with $_GET or $_POST. If you need to get other parts of the URL, you can use parse_url()

  2. While file_get_contents() will work, cURL is a much more sophisticated and powerful solution.

  3. cURL can return a file handle containing whatever file has been downloaded, and it can be processed at that point.

  4. You can then use fwrite() to write the file contents to a local file

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
0

this is an example and work for jpeg images only.

<?
$url = $_GET['url'];

$im = imagecreatefromjpeg($url);
header("Content-Type: image/jpeg");

for($i=0;$i<100;$i++)
{
    for($j=0;$j<100;$j++)
    {
        if(rand(0,1))
        {
            $rand = rand(0,4);
            imagesetpixel($im,$i,$j,imagecolorallocate($im, 255, 255, 255));
        }
    }
}

imagejpeg($im);
?>
alpera
  • 509
  • 4
  • 9
0

I like python for this sort of thing. Specifically, a Python WSGI application, sitting on a WSGI server should be fine for your application.

Python comes with a "simple server" upon which you can test. For deployment, you may want to find third party WSGI servers, like CherryPy's WSGI server or Rocket (both implemented entirely in python).

I would recommend python 3.2 if possible, as the GIL is much better, and this has an impact on multi-threaded WSGI servers like the ones I mentioned - CherryPy and Rocket.

Read up on WSGI for more information. At the Python sit e, PEP 333 and 3333.

Mayur Patel
  • 945
  • 1
  • 7
  • 15
  • I just did a quick check on `nearlyfreespeech.net`'s support of WSGI and it doesn't look like it's well supported, if at all. From their forums: `I realize that without mod_python or WSGI, Python is suboptimal, and indeed an equivalent script in PHP is almost instantaneous.` – Jeff Axelrod Aug 17 '12 at 19:15
0

Here's how I did it in Perl:

#!/usr/bin/perl
use CGI qw(:standard);
use IO::Handle;
use LWP::Simple;
use File::Temp;
use File::Slurp;

$url = param('url');
$width = param('width');
$height = param('height');
$content = get($url);

$in = File::Temp->new( SUFFIX => '.jpg' );
print $in $content;

$out = File::Temp->new( SUFFIX => '.jpg' );
system("convert $in -resize $width" . "x" . "$height $out");

my $q = new CGI;
print $q->header( - type => "image/jpeg", -expires => "-1d" );

$content2 = read_file($out);
print $content2;
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246