Solved

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

Posted on 2010-09-12
15
592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
LVL 110

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 110

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 110

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
 

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 110

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 110

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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