Jump to content

Multiple Image Output in GD PHP


Recommended Posts

Notes

I have the issue resolved with one image in GD and I could easily copy that code 5 or 6 times to get multiple images to output however I would like to do this a little easier but and running into a road block.  This problem I am sure is due to my lack of experience in programming with GD.

 

Issue

I am attempting to use an array and a for statement to output multiple images in GD as is done with anything else I have worked with.  When I attempt to run an array of images via a FOR loop I get no image and no multiple images.

 

URL

Broken Code URL: Spectacular Computer Repair OR http://67.199.62.141/test3.php whichever you prefer.

 

Correct Code to OUTPUT 1 Image URL: Computer Repair Sarasota, Bradenton, Venice OR http://67.199.62.141/test2.php whichever you prefer.

 

Broken Code

<?php

function open_image($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
}

header ('Content-Type: image/png'); // What type of image are we working with?  I also need to learn how to output multiple headers if I am using different images.

//$image = open_image('SCRFix-logo.png'); // Where is the image located?

//Image array and their correct locations.
$image = array("/images/scrfix-logo.png", "/images/shopping-cart.jpg", "/images/forums.jpg", "/images/sign-in.jpg", "/images/contactus.jpg", "/images/printer.jpg");

//Check to see if we can find the image
if ($image === false) {
        die ('Unable to find image');
}
for ($i = 1; $i < 6; $i++) {
$image = open_image($image);
imagepng($image); // Outputs to browser directly only once through GD Code however this doesn't return an image.
}

//imagejpeg($image);  //Here for testing purposes because I have multiple different types of images, both png and jpg for quick loading.
?>

 

Correct Code to OUTPUT 1 Image

<?php

function open_image($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
}

header ('Content-Type: image/png'); // What type of image are we working with?

$image = open_image('SCRFix-logo.png'); // Where is the image located? what is the image named?  Do not use a / in the beginning of the location ie. /images/SCRFix-Logo.png is a no no.  Use images/SCRFix-logo.png

//Check to see if we can find the image
if ($image === false) {
        die ('Unable to find image');
}
imagepng($image);
?>

 

Any help would be appreciative.  You can find all of the images I am working with here

Image Locations

http://67.199.62.141/images/scrfix-logo.png

http://67.199.62.141/images/shopping-cart.jpg

http://67.199.62.141/images/forums.jpg

http://67.199.62.141/images/sign-in.jpg

http://67.199.62.141/images/contactus.jpg

http://67.199.62.141/images/printer.jpg

 

Thanks,

 

Wayne

Link to comment
Share on other sites

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.