Solved

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

Posted on 2010-09-12
15
578 Views
Last Modified: 2012-06-21
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
Comment
Question by:peps03
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 3

Expert Comment

by:T1750
ID: 33657801
Just make a system call to imagemagick and let that do it.
0
 

Author Comment

by:peps03
ID: 33657819
i'm looking for a script
0
 
LVL 3

Expert Comment

by:T1750
ID: 33657846
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33657931
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
 

Author Comment

by:peps03
ID: 33657984
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33658092
The key question here is "why not create the thumbnail on the server?"  What do you want to do with the thumbnail?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33658118
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:peps03
ID: 33658228
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
 

Author Comment

by:peps03
ID: 33658230
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
 

Author Comment

by:peps03
ID: 33658253
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33658291
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
 

Author Comment

by:peps03
ID: 33658345
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33658561
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
 
LVL 6

Expert Comment

by:Mike D
ID: 33669993
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
 

Accepted Solution

by:
peps03 earned 0 total points
ID: 33670126
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now