Re: resize image and store it to DB
| From: | Tijnema ! | Date: | Tue, 01 May 2007 10:59:59 +0000 |
| Subject: | Re: resize image and store it to DB | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-254181@lists.php.net to get a copy of this message | ||
On 5/1/07, Alain Roger <raf.news@gmail.com> wrote:
Hi, I allow web application users to insert their own picture into database. but i defined a max size limit 130 px width by 160px height. so for that i determine the max ratio in case of picture does not have this size. after that i resize it and would like to store it to DB, however i have some problem once it is resized. here is my code : data = file_get_contents($_FILES['uploadedfile']['tmp_name']); $img = imagecreatefromstring($data); $width = imagesx($img); $height = imagesy($img); $maxwidth = 130; //130px $maxheight = 160; //160px $ratio =0.00;I don't know if there's a better way, but you could try output buffering, so that you start it, output the image with imagejpeg, imagepng, imagegif, etc. and then get the contents of the buffer. Tijnemaif($width >$maxwidth || $height>$maxheight) // image is somehowbigger than 160px * 130px { if($width >$maxwidth) { $ratio = $maxwidth/$width; } if($height>$maxheight) { // always take the smallest ratio $ratio = ($maxheight/$height) > $ratio ? $ratio : ($maxheight/$height); } } else // both $width and $height are smaller than max, so we need to zoom { $i=0.00; $i=($maxwidth/$width); $i = ($maxheight/$height) > $i ? $i : ($maxheight/$height); $ratio = $i; } $newwidth = $width*$ratio; $newheight = $height*$ratio; $thumb = imagecreatetruecolor($newwidth, $newheight); imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $escaped = pg_escape_bytea($thumb); // does not work and it's normal $thumb is an image, so ho can i make it useful for pg_escape_bytea function ? i do not want to create a tmp file on server and after load it. I would like to do it stored image directly into DB on-fly. thanks alot, -- Alain