Re: resize image and store it to DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/1/07, Alain Roger <raf.news@xxxxxxxxx> 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;
> if($width >$maxwidth || $height>$maxheight)        // image is somehow
> bigger 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


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.

Tijnema

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux