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

前端 未结 4 944
攒了一身酷
攒了一身酷 2021-01-19 20:36

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

相关标签:
4条回答
  • 2021-01-19 20:48

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

    0 讨论(0)
  • 2021-01-19 20:50

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

    0 讨论(0)
  • 2021-01-19 20:54

    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.

    0 讨论(0)
  • 2021-01-19 21:08

    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.

    0 讨论(0)
提交回复
热议问题