4

I have created an image by copying a number of images into a new one. In the last step of my program, I am trying to export this file into a folder.

The code is as follows:

<?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');

        $newHeight = 0;

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

            $width[$iii] = imagesx($img[$iii]);
            $height[$iii] = imagesy($img[$iii]);

            $newHeight += ($height[$iii] + 30);
        }

        $newWidth = max($width);
        $cummDestHeight = 0;

        $export = imagecreatetruecolor($newWidth, $newHeight);
        imagefill($export, 0,0, 0xFFFFFF);

        for($iii=0;$iii<count($img);$iii++){
            imagecopy($export, $img[$iii], 0, $cummDestHeight, 0, 0, $width[$iii], $height[$iii]);
            $cummDestHeight += $height[$iii] + 30;
        }


        $bits = explode('/',$next);
        file_put_contents("../pidjin/$bits[5]-$bits[4]-$bits[3].png",$export);
?>

The error that I receive is this:

Warning: file_put_contents(): supplied resource is not a valid stream resource in E:\Web\Comics\pidjin.php on line 54

Problem: I am not sure how I can make $export a valid stream resource.

day0
  • 497
  • 2
  • 5
  • 10

4 Answers4

5

$export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image..

For that, you should be doing

imagepng($export, "../pidjin/$bits etc...");

which will create the .PNG file for you.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thank you very much. That clarified a huge confusion that I had with GD. Thanks again. – day0 Aug 29 '12 at 20:10
1

With another question, I was able to finally get the code working.

Solution: The problem was that I was trying to use the file_put_contents to dump a GD handle and, as it turned out, it is not as simple as that. I was directed to the imagepng function which would take the directory as the second argument for exporting the file.

Program: I made a program to download the strips from the webcomic Fredo and Pidjin so that I can read it later when I don't have access to the internet. The program is given below.

<?php

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

$next = "http://www.pidjin.net/2006/02/19/goofy-monday/";

$counter = 1;
while($next){

    $html = file_get_html($next);

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

    $newHeight = 0;

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

        $width[$iii] = imagesx($img[$iii]);
        $height[$iii] = imagesy($img[$iii]);

        $newHeight += ($height[$iii] + 30);
    }

    $newWidth = max($width);
    $cummDestHeight = 0;

    $export = imagecreatetruecolor($newWidth, $newHeight);
    imagefill($export, 0,0, 0xFFFFFF);

    for($iii=0;$iii<count($img);$iii++){
        imagecopy($export, $img[$iii], 0, $cummDestHeight, 0, 0, $width[$iii], $height[$iii]);
        $cummDestHeight += $height[$iii] + 30;
    }


    $bits = explode('/',$next);

    imagepng($export, "../pidjin/$counter ($bits[5]-$bits[4]-$bits[3]).png");

    $nextUrl = $html->find('span[class=next] a[rel=next]');
    $next = $nextUrl[0]->href;
    $counter++;
}

?>

Note: I have used the Simple HTML DOM Parser to scrape the source and look through the DOM.

Cheers.

day0
  • 497
  • 2
  • 5
  • 10
  • What is the point of all this (utterly useless) GD work? You get an image from a site, you make an exact copy of that image to another image, save that new image. Instead, you could just directly save the original image you fetched without ever having to decompress it. – Marc B Sep 01 '12 at 16:34
1

Just replaced "file_put_contents" with "imagejpeg($rotate, $file_new);"

शु-Bham
  • 952
  • 1
  • 7
  • 14
-1

file_put_contents as per PHP manual takes the second argument a string. An image file is not a string. See the two other answeres above. That is how you save images. Try using the manual a bit more.

transilvlad
  • 13,974
  • 13
  • 45
  • 80