iceman023 Posted June 24, 2009 Share Posted June 24, 2009 I already have it setup where someone can add a photo to the database and it displays but its too large.. i found this code online.. what do i have to change to make it 500px width.. What do you think? <? function smart_resize_image( $file, $width = 0, $height = 0, $proportional = true, $output = 'browser', $delete_original = false, $use_linux_commands = false, $apply_watermark = true ) { if ( $height <= 0 && $width <= 0 ) return false; # Setting defaults and meta $info = getimagesize($file); $image = ''; $final_width = 0; $final_height = 0; list($width_old, $height_old) = $info; # Calculating proportionality if ($proportional) { if ($width == 0) $factor = $height/$height_old; elseif ($height == 0) $factor = $width/$width_old; else $factor = min( $width / $width_old, $height / $height_old ); $final_width = round( $width_old * $factor ); $final_height = round( $height_old * $factor ); } else { $final_width = ( $width <= 0 ) ? $width_old : $width; $final_height = ( $height <= 0 ) ? $height_old : $height; } # Loading image to memory according to type switch ( $info[2] ) { case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break; default: return false; } # This is the resizing/resampling/transparency-preserving magic $image_resized = imagecreatetruecolor( $final_width, $final_height ); if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) { $transparency = imagecolortransparent($image); if ($transparency >= 0) { $transparent_color = imagecolorsforindex($image, $trnprt_indx); $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($image_resized, 0, 0, $transparency); imagecolortransparent($image_resized, $transparency); } elseif ($info[2] == IMAGETYPE_PNG) { imagealphablending($image_resized, false); $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); imagefill($image_resized, 0, 0, $color); imagesavealpha($image_resized, true); } } // this wasy replaced at the bottom watermark //imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); # Taking care of original, if needed if ( $delete_original ) { if ( $use_linux_commands ) exec('rm '.$file); else @unlink($file); } # Preparing a method of providing result switch ( strtolower($output) ) { case 'browser': $mime = image_type_to_mime_type($info[2]); header("Content-type: $mime"); $output = NULL; break; case 'file': $output = $file; break; case 'return': return $image_resized; break; default: break; } # Writing image according to type to the output destination switch ( $info[2] ) { case IMAGETYPE_GIF: imagegif($image_resized, $output); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break; case IMAGETYPE_PNG: imagepng($image_resized, $output); break; default: return false; } return true; } echo $info ; ?> <?php if ($apply_watermark) { $outputWidth = floor($rawWidth * $imageScaleTo); $outputHeight = floor($rawHeight * $imageScaleTo); $outputImage = Imagecreatetruecolor($final_width, $final_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); // Turn on alpha blending imagealphablending($outputImage, true); // Create overlay image - Could be a png? $watermark = imagecreatefromgif("../images/watermark.gif"); // Get the offset centering for the overlay $offsetWidth = floor(($final_width - imagesx($watermark))/2); $offsetHeight = floor(($final_height - imagesy($watermark))/2); // Overlay watermark imagecopy($image_resized, $watermark, $offsetWidth, $offsetHeight, 0, 0, imagesx($watermark), imagesy($watermark)); } else { imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/ Share on other sites More sharing options...
brooksh Posted June 25, 2009 Share Posted June 25, 2009 resize_img(yourfile.jpg, "500"); function resize_img($filename, $newwidth){ // Set a maximum height and width $width = $newwidth; $height = 600; //max header('Content-type: image/jpeg'); list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, $filename, 70); }//end function resize_img Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863046 Share on other sites More sharing options...
iceman023 Posted June 25, 2009 Author Share Posted June 25, 2009 ISNT THAT ONLY FOR ONE FILE THO... OR EVERYTIME A FILE IS UPLOADED.. AND I WOULD LIKE TO USE IT MY WIDTH NOT HEIGHT... Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863054 Share on other sites More sharing options...
killah Posted June 25, 2009 Share Posted June 25, 2009 WHATS WITH ALL THE CAPS? Do you like shouting? :/ Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863056 Share on other sites More sharing options...
.josh Posted June 25, 2009 Share Posted June 25, 2009 I'm not going to bother picking apart some 3rd party function you picked up, but just looking at the name of the function and the arguments being passed to it, it looks like all you need to do is pass the information you want to it, including the width you want. Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863078 Share on other sites More sharing options...
garethhall Posted June 25, 2009 Share Posted June 25, 2009 Dude take it from me the GD sucks man. If you try to do a very large image like 4000px by 4000px it will fail due to the limitation on the memory limit in php. Look into imagemagick It's faster and needs less scripting for the most part. Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863079 Share on other sites More sharing options...
.josh Posted June 25, 2009 Share Posted June 25, 2009 There is nothing wrong with the GD library or with php in general when it comes to graphics. What's wrong is the people who try to use php as a solution for their graphics needs. It is not meant to be some full blown graphic manipulation language. That sort of language belongs in an environment where you can dedicate a lot of ram and processing power and stuff to it. That is not a server environment. php is a language built to be run on a server, for the web. From that context, GD is actually quite impressive. Quote Link to comment https://forums.phpfreaks.com/topic/163574-solved-php-gd-image-resize-and-watermarking-help/#findComment-863292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.