Solved

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

Posted on 2010-09-12
15
571 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
Comment Utility
Just make a system call to imagemagick and let that do it.
0
 

Author Comment

by:peps03
Comment Utility
i'm looking for a script
0
 
LVL 3

Expert Comment

by:T1750
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:peps03
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:johndoeherty
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

9 Experts available now in Live!

Get 1:1 Help Now