Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 620
  • Last Modified:

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!
 

0
peps03
Asked:
peps03
  • 7
  • 5
  • 2
  • +1
1 Solution
 
T1750Commented:
Just make a system call to imagemagick and let that do it.
0
 
peps03Author Commented:
i'm looking for a script
0
 
T1750Commented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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?
0
 
peps03Author Commented:
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/
0
 
Ray PaseurCommented:
The key question here is "why not create the thumbnail on the server?"  What do you want to do with the thumbnail?
0
 
Ray PaseurCommented:
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

0
 
peps03Author Commented:
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

0
 
peps03Author Commented:
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 />";
0
 
peps03Author Commented:
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 />";
0
 
Ray PaseurCommented:
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.
0
 
peps03Author Commented:
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..
0
 
Ray PaseurCommented:
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
0
 
Mike DSolution ConsultantCommented:
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
0
 
peps03Author Commented:
hi john & ray,

Thanks for you comments and help. because of what ray said:
"If you don't want to do that, you have an "interesting" challenge ahead of you"

So i spent some (a lot) more hours on the imgsize.php script to get it to work. which it finally did. My local host doesn't support the gd-library, i finally found out.., that was issue one.
and because i wanted the imgsize.php to show the possible errors, i turned error-reporting on in the script, but that messed up the script as well.
now its working correct.

the reason why i didn't want to use thumbnails is because i have a script running that lets the users upload pictures via a cms to a folder, and the folder is scanned and the pictures are shown in a lightbox.
so in order for the users to keep overview i didn't want then to also have to remember deleting thumbnails and folder when deleting of moving folders.
the site i was talking about is waitingforlenny.com  (menu "See")

but this imgsize.php script does the trick.. finally..

thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now