Jump to content

Recommended Posts

Hi there,

 

I've hit a bit of a roadblock on something I've been working on, was wondering if anyone knew of a way to fix the error message:

 

imagecreatefromjpeg():php_network_getaddresses: getaddrinfo failed

 

The code I'm using is below, the script basically grabs a certain image from a set of specific urls and resizes it as close to 600x450 as it can, then saves the image/images in a zip file which the user can then download.

 

I should also mention that I'm currently hosting this on x10hosting, and it runs a-okay locally.

<?php

include 'zip.php';

function getImage($tags){
$count = 0;

    foreach ($tags as $tag){
        $tag->getAttribute('src');
        $count += 1;

        if ($count == 3){
            $old_image = $tag->getAttribute('src');
            return $old_image;
        }
    }
}

function deleteFiles($random_name){
    $images= scandir("$random_name"); //get an array of files
    unset($images[0], $images[1]);
    
    // loop through files
    foreach($images as $image){
        unlink("$random_name/$image"); // delete each file
    }

    rmdir($random_name);
}

$omitted_urls = [];


if (isset($_POST['submit'])){
    $start_time = time();

    $urls = $_POST['urls']; // take inputted urls
    $urls = explode("\r\n", $urls); // break them up by a carriage return/new line into array

    $names = $_POST['names']; // take inputted names
    $names = explode("\r\n", $names); // break them up by a carriage return/new line into array

    $random_name = "pics".rand(0,1000); // create a random folder name
    if (!is_dir("$random_name")){
        mkdir("$random_name", 0777);
    }

    // for each url
    for($i=0; $i < count($urls); $i++){


        @$html = file_get_contents($urls[$i]); // get the contents of the url

        $doc = new DOMDocument();
        @$doc->loadHTML($html); // load up the html file
        $tags = $doc->getElementsByTagName('img'); // look for all img tags
        $count = 0;

        $special_chars = array('\\','/',':','*','?','"','<','>','|'); // look for any special characters

        $names[$i] = str_replace($special_chars, "", $names[$i]); // replace them with ""
        $names[$i] = substr($names[$i], 0, 50); // truncate anything over 50 characters

        if($old_image = imagecreatefromjpeg(getImage($tags))){ // create the old image

            echo "The src is ".getImage($tags)."<br>";

            $old_width = imagesx($old_image);  // get the width of the old image
            $old_height = imagesy($old_image); // get the height of the old image

            $ratio_orig = $old_width/$old_height; // work out the ratio of the image

            $new_width = 600; $new_height = 450;

            if ($new_width/$new_height > $ratio_orig){
                $new_width = $new_height*$ratio_orig;
            } else {
                $new_height = $new_width/$ratio_orig;
            }

            $new_image = imagecreatetruecolor($new_width, $new_height); // creates a black image to represent size
            imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); // copy over old image to fit new image

            imagejpeg($new_image, "$random_name/".$names[$i].'.jpg'); // create the new image file
            imagedestroy($new_image); // destroy the image file
            $time = time()- 3600; // find out the time

            } else {
                $omitted_urls[] = $names[$i];
            }

        }

    $final_time =  time() - $start_time; // final time is the current time take the starting time
    $final_time = date("i:s", $final_time); // time taken
}

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>EGI Research - Resize and Extract Images</title>
        <link href="/RESEARCH/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        <link href="css/style.css" rel="stylesheet"/>

        <script src="/RESEARCH/bundles/modernizr?v=rGcoDow97GYrNMSwHq7xCCjlcB3UIY4_OhPRc6BBSQA1"></script>

    </head>
    <body>
        <header>
            <body>
        <header>
            <div class="content-wrapper">
            </div>
        </header>
        </header>
        <div id="body">
            
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>URL Image Extractor & Resizer</h1>
            </hgroup>

            <?php if(isset($_POST['submit'])){
            printf("<br><b> %s images were downloaded and resized in %s seconds </b><br><br>", count(scandir($random_name))-2, $final_time);

            if (count($omitted_urls) > 0){
                foreach($omitted_urls as $omitted_url){
                    echo "$omitted_url was omitted<br>";
                }
            }

            createZip($random_name);
            deleteFiles($random_name);
            echo "<a href='$random_name.zip'>Click here to download images</a>";
            }?>
       
        </div>
    </section>

            <section class="content-wrapper main-content clear-fix">
                

<form action="" id="UploadForm" method="post"><div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li

</ul></div>    <ol class="round">
        <li class="one">
            <h5>URLS</h5>
            <p>Enter a set of EGi URLS</p>
            <textarea name="urls" rows="4"></textarea>
        </li>
        <li class="two">
            <h5>File Names</h5>
            <p>
            Enter a set of unique identifiable File Names
            </p>
            <textarea name="names" rows="4"></textarea>
            <br>
            <input type="submit" value="Extract & Resize" name="submit"/><a href="index2.php">Clear Form</a>
        </li>
    </ol>
</form>

            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>© 2013 - Reed Business Information Ltd.</p>
                </div>
            </div>
        </footer>

        
    </body>
</html>

Thanks for any of your know how.

Edited by KapaGino

The urls themselves would look like this:

http://www.estatesgazette.com/propertylink/advert/blackfriars_road_london-london-3475714.htm

http://www.estatesgazette.com/propertylink/advert/borough_high_street_london-london-3476800.htm
Edited by KapaGino

1) the @ error suppressors you have in your code are probably hiding related errors, that the web host doesn't permit reading external urls. you need to remove all the @ error suppressors and have php's error_reporting set to E_ALL so that you know everything that is not working in the code.

 

2) have you echoed what getImage($tags) does return so that you know it is an actual complete expected value?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.