Link to home
Create AccountLog in
Avatar of peps03
peps03

asked on

php image resize script, no thumbnail, reduce size as well.

Hi,

I'm looking for a script that resizes big images to a thumbnail format. but i DON"T want the script to create the actual thumbnails.
I also would like the script to reduce the filesize of the smaller image.

i've searched the EE-database, and couldn't find a suitable one.

i've also tried this one: http://tech.mikelopez.info/2006/03/02/php-image-resize-script/
but i can't get this script working bad enough..  But this was a script that fits my needs.

and i couldn't find any good ones with google.

any help is appreciated!
 

Avatar of T1750
T1750

Just make a system call to imagemagick and let that do it.
Avatar of peps03

ASKER

i'm looking for a script
OK, but you said any help would be appreciated and you could do it in like 5 minutes if you just called imagemagick.

I am a python programmer, if I have a large amount of text I'm working with I don't process it with python I make system calls to awk/grep/sed because it runs faster, it's less work to code and saves duplicating other peoples effort.
Regarding this:

"...resizes big images to a thumbnail format. but i DON"T want the script to create the actual thumbnails."

Can you please explain why you wrote that?  How would you envision using a thumbnail if you did not create the thumbnail?
Avatar of peps03

ASKER

a script  that shrinks pictures to 100x75 px (thumbnail format)

but doesn't create a real thumbnail on the server....

like the script i couldn't get to work..
http://tech.mikelopez.info/2006/03/02/php-image-resize-script/
The key question here is "why not create the thumbnail on the server?"  What do you want to do with the thumbnail?
This script shows the principles.  Please see the man page here:
http://us2.php.net/manual/en/function.imagejpeg.php

You might adjust the imagejpeg() function call on line 76 if you want to output the new image directly to the browser.

<?php // RAY_image_resize.php RESIZE IT TO FIT A PREDEFINED SIZE


// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE)
{
    // IS GD INSTALLED AT ALL?
    if (!function_exists("gd_info"))
    {
        if ($display) echo "<br/>GD NOT INSTALLED\n";
        return FALSE;
    }

    // IF GD IS INSTALLED GET DETAILS
    $gd = gd_info();

    // IF DISPLAY IS REQUESTED, PRINT DETAILS
    if ($display)
    {
        echo "<br/>GD DETAILS:\n";
        foreach ($gd as $key => $value)
        {
            if ($value === TRUE)  $value = 'YES';
            if ($value === FALSE) $value = 'NO';
            echo "<br/>$key = $value \n";
        }
    }

    // RETURN THE VERSION NUMBER
    $gd_version = preg_replace('/[^0-9\.]/', '', $gd["GD Version"]);
    return $gd_version;
}


// A FUNCTION TO MAKE AN IMAGE INTO THE RIGHT WIDTH FOR PAGE DISPLAY
// WILL WORK IF GD2 NOT INSTALLED, BUT WILL MAKE BETTER IMAGES WITH GD2
// INPUT IS THE IMAGE FILE NAME, OUTPUT IS AN IMAGE RESOURCE, OR FALSE IF NO RESIZE NEEDED
function create_right_size_image($image, $width=300)
{
    // IS GD HERE?
    $gdv = get_gd_info();
    if (!$gdv) return FALSE;

    // GET AN IMAGE THING
    $source = imagecreatefromjpeg("$image");

    // GET THE X AND Y DIMENSIONS
    $imageX = imagesx($source);
    $imageY = imagesy($source);

    // IF NO RESIZING IS NEEDED
    if ($imageX <= $width)
    {
        return FALSE;
    }

    // THE WIDTH IS TOO GREAT - MUST RESIZE
    $tnailX = $width;
    $tnailY = (int) (($tnailX * $imageY) / $imageX );

    // WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
    if ($gdv >= 2)
    {
        // IF GD IS AT LEVEL 2 OR ABOVE
        $target = imagecreatetruecolor($tnailX, $tnailY);
        imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
    }
    else
    {
        // IF GD IS AT A LOWER REVISION LEVEL
        $target = imagecreate($tnailX, $tnailY);
        imagecopyresized   ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
    }
    return $target;
}


// SAMPLE CALLING SEQUENCE
// RESIZE THE FILE TO FIT PAGE WIDTH, IF NECESSARY
if ($imageblob = create_right_size_image($my_image_file))
{
    // CREATE YOUR NEW FILE NAME HERE?
    $my_new_image_file = 'YOUR FILE NAME';

    // STORES THE IMAGE IN A NEW FILE NAME
    imagejpeg($imageblob, $my_new_image_file);
}

Open in new window

Avatar of peps03

ASKER

thanks ray!

how can i get that script working with this line of code that i use to get the image:

thanks!
<?
"<a href='" . $dir . "/" . $file . "/" . $filen . "' rel='lightbox-mobile[album_" . $album . "]' title='Lenny's Album: " . $file . "' class='page'><div style='width:75px; display:inline-block;'><img src='pics.php?p=" . $dir . "/" . $file . "/" . $filen . "&w=100&h=100'  STYLE='border: solid 1px black;'></div>&nbsp;&nbsp;". $file ."</a><br />";?>

Open in new window

Avatar of peps03

ASKER

this is the same code:

"<a href='" . $dir . "/" . $file . "/" . $filen . "' rel='lightbox-mobile[album_" . $album . "]' title='Lenny's Album: " . $file . "' class='page'><div style='width:75px; display:inline-block;'><img src='pics.php?p=" . $dir . "/" . $file . "/" . $filen . "&w=100&h=100'  STYLE='border: solid 1px black;'></div>&nbsp;&nbsp;". $file ."</a><br />";
Avatar of peps03

ASKER

hmmm this is the correct line, the code above is wrong, still had the code of the script in it from the link above...

sorry. couldn't edit the posts above!

right code:
print "<a href='" . $dir . "/" . $file . "/" . $filen . "' rel='lightbox-mobile[album_" . $album . "]' title='Lenny's Album: " . $file . "' class='page'><div style='width:75px; display:inline-block;'><img src='" . $dir . "/" . $file . "/" . $filen . "'  STYLE='border: solid 1px black;'></div>&nbsp;&nbsp;". $file ."</a><br />";
Sorry - I don't have any idea.  My advice would be to write the thumbnail image onto your server so you can use it in the <img> tag.  If you don't want to do that, you have an "interesting" challenge ahead of you.
Avatar of peps03

ASKER

thanks anyway.
i thought it would be difficult.. thats why i tried this script:
http://tech.mikelopez.info/2006/03/02/php-image-resize-script/
(also posted above)

so i only had to insert this:
imgsize.php?h=100&img=path/to/image.jpg

but for some reason its not working..
These pidgin-code fragments are almost worthless in the effort to understand what your scripts are doing.  If you can isolate a test case and post the entire test-case script we will have a much better chance of helping you.  There are just too many moving parts that remain invisible.

And give some thought to trying to work with our questions and suggestions.  You never answered the vitally important question, "why not create the thumbnail on the server?"  A common-sense solution to your issues really needs the thumbnail on the server.  But if you have a really good reason why you cannot do that, we need to understand it in order to advise you.

Also, "its not working" is not an error message.  We need to see the code and the error messages.

Anyway, you have received a tested and working code example that teaches how to achieve what you need.  If you have any questions or can answer any of our questions, please post back.

Best of luck with your project, ~Ray
I can only second Ray's post.
You are aware that the thumbnail will always be created in a temp folder on your server (where do you think the data sent to the browser comes from when you issue 'imgsize.php?h=100&img=path/to/image.jpg'?).

It sounds more like your image generation isn't working correctly but without any error messages etc. to go on...

JD
ASKER CERTIFIED SOLUTION
Avatar of peps03
peps03

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account