day0
day0

Reputation: 497

file_put_contents() is issuing an error when trying to export an image

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.

Upvotes: 4

Views: 6217

Answers (4)

शु-Bham
शु-Bham

Reputation: 952

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

Upvotes: 1

transilvlad
transilvlad

Reputation: 14532

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.

Upvotes: -1

day0
day0

Reputation: 497

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.

Upvotes: 1

Marc B
Marc B

Reputation: 360792

$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.

Upvotes: 5

Related Questions