Page 1 of 1

The pictures ugly

Posted: Fri Oct 11, 2002 3:10 pm
by ace2600
The code on my site takes an image and resizes it if its too large. However the resulting image is pixely; its worse than it should be for the resize. Is their another function or what that can do what I need with better quality?

Code: Select all

function resize_jpeg( $image_file_path, $new_image_file_path, $max_width, $max_height) 
{ 
     
    $return_val = 1; 
     
    $return_val = ( ($img = ImageCreateFromJPEG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0"; 
     
    $FullImage_width = imagesx ($img);    // Original image width 
    $FullImage_height = imagesy ($img);    // Original image height 


// now we check for over-sized images and pare them down 
// to the dimensions we need for display purposes 
    $ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ; 
    $new_width = ((int)($FullImage_width * $ratio));    //full-size width 
    $new_height = ((int)($FullImage_height * $ratio));    //full-size height 
            //check for images that are still too high 
    $ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ; 
    $new_width = ((int)($new_width * $ratio));    //mid-size width 
    $new_height = ((int)($new_height * $ratio));    //mid-size height 

     
// --Start Full Creation, Copying-- 
// now, before we get silly and 'resize' an image that doesn't need it... 
    if ( $new_width == $FullImage_width && $new_height == $FullImage_height ) 
        copy ( $image_file_path, $new_image_file_path ); 
     
    $full_id = ImageCreate($new_width , $new_height );        //create an image 
    ImageCopyResized( $full_id, $img, 
                    0,0,  0,0, //starting points 
                    $new_width, $new_height, 
                    $FullImage_width, $FullImage_height ); 
    $return_val = ( $full = ImageJPEG( $full_id, $new_image_file_path, 80 ) 
                 && $return_val == 1 ) ? "1" : "0"; 
    ImageDestroy( $full_id ); 
// --End Creation, Copying-- 
     
    return ($return_val) ? TRUE : FALSE ; 
     
} 

?>
I got the code from zend.
All help is appreciated!
Ace

Posted: Fri Oct 11, 2002 4:49 pm
by DSM
Your best bet is to see if your server/host has Image Magick on the server. It is a much better way to deal with converting/resizing already present images. GD tends to degrade images when it does resizing or ratio resizes. Look into Image Magick at http://www.imagemagick.org

Posted: Sun Oct 13, 2002 11:52 am
by ace2600
Okay I now have image magick installed, but have no clue how to use it. I'm looking for sometype of tutorials or code I can use, using Image Magick that will resize an image and save it as whatever I name it. Image Magick's tutorials on their website showed the syntax but gave no examples of it's implementation.
All help is appreciated!
-Ace

Posted: Sun Oct 13, 2002 4:58 pm
by DSM
You call Image Magick thru the exec() function of php, this allows you to call outside applications.

Code: Select all

<?php
exec("convert //action you want IM to do -size //Tells it to set to size $thumbsize //Thumbsize variable $abpath/$img1_orig//absoulte path to orig image -resize// Resize $thumbsize//To thumbsize  $abpath/$img1_thumbnail_name//absolute path and new file name of resized image"); 

?>

Posted: Sun Oct 13, 2002 7:21 pm
by ace2600
Why can't this just work, its driving me crazy!
I did what DSM said (thanks for the help by the way DSM) and although im not getting any errors, the new image does not upload to the site. Here is my code, PLEASE HELP!

Code: Select all

//$temp stri holds info for the exec command
$temp_str = "convert -size 250x100 $imgfile1 -resize 250x100 $img1";

//I will show the output of this command in a sec
echo "Temp String = $temp_str";

//the actual execution
exec($temp_str);
Heres the output from the 'echo "Temp String = "$temp_str";':
"Temp String = convert -size 250x100 /tmp/phpxd77x2 -resize 250x100 ./images/142a.jpg".
Could someone please help.
-Ace

Posted: Mon Oct 14, 2002 7:05 am
by DSM
Ok, Paths have to be absolute...

This is how I do it...

Code: Select all

<?php
$abpath = "/home/my domain/www/html/images/upload"; //Absolute path to where images are uploaded. No trailing slash

$im_convert = "/usr/bin/convert"; //Where Image Magick resides on the server, and the command I want to run


$thumbsize = "75x75";
$mediumsize = "200x200";
$largesize = "600x600";

$img_1_orig = "my_pic.jpg";
$img1_thumbnail_name = "my_thumb.jpg";

exec("$im_convert -size $thumbsize $abpath/$img1_orig -resize $thumbsize  $abpath/$img1_thumbnail_name");


?>
Upload your image, copy it, rename it, then run IM on it.

If you still have probs, email me and I'll help out, but i will not do it for you. It took me a few tries to get it right.