0

I am trying to create a small experimental script for obtaining comic strips from web-comic sites using php. The site that I am playing with is Fredo and Pidjin. Here is the code that I have written so far:

<?php

        require_once "../shdp/simple_html_dom.php";

        $next = "http://www.pidjin.net/2012/08/28/of-my-own/";
        $html = file_get_html($next);

        $imageList = $html->find('div[class=episode] p img');

        for($iii=0; $iii<count($imageList); $iii++){
            $storage[$iii] = $imageList[$iii]->src;

        }

        $img = file_get_contents($storage[0]);
        $img = imagecreatefromstring($img);

        header("Content-type: image/png");
        $something = imagepng($img);
?>

For scraping the html, I am using the Simple HTML DOM parser.

This is what I'm trying to do: Get the src of the image and assign it to a handle. Subsequently find it for all the comic panels on the page and use imagecopy to make one strip that can be saved to the computer. The code, however, is in the preliminary stages and I have not got to the imagecopy part yet.

Problem: While imagepng(...) outputs the file on the browser, I am not able to get a $src handle (so to speak) on the image to use in imagecopy.

Thanks in advance.

j08691
  • 204,283
  • 31
  • 260
  • 272
day0
  • 497
  • 2
  • 5
  • 10

1 Answers1

2

How are you trying to get a handle to the image?

$src = imagecreatefrompng($storage[0]);
$dest = imagecreatetruecolor(80, 40);
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

header('Content-Type: image/png'); 
imagepng($dest);

In case you need to show multiple images try with output buffering:

ob_start();
header('Content-Type: image/png'); 
imagepng($img1);

header('Content-Type: image/png'); 
imagepng($img2);
ob_end_flush();
raidenace
  • 12,789
  • 1
  • 32
  • 35
  • After getting $src in the method that you suggested, how do I display it on the browser ? With echo($src), the browser displays "Resource id #6". I feel entirely confused with this right now. – day0 Aug 29 '12 at 15:16
  • `imagecopy` is used to *copy a part of an image* and what it returns is a resource which contains the copied portion. In order to show it in the browser, all you do is `header('Content-Type: image/png'); imagepng($dest);`. Is this what you are trying to achieve? – raidenace Aug 29 '12 at 15:21
  • Thanks a lot for your help so far. Have been able to make some headway into the matter. I am facing another issue at the moment, I have two resources `$img1` and `img2`. I tried giving two consecutive `imagepng($resource)` but only the first image was displayed in the browser. – day0 Aug 29 '12 at 18:21
  • Try using output buffering for such situations, have updated my answer for it, check if that helps – raidenace Aug 29 '12 at 19:55
  • Thanks a lot. I'll try that out. I've completed the script that I wanted to dish out and have posted it here http://stackoverflow.com/a/12193167/1623428. Thanks again for your help. – day0 Aug 30 '12 at 09:11