Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLOB - PHP re-size Image

I am having a small issue re-sizing blob images. What I have found is I have to do height and width of the BLOB but because people upload images that are not square, how do I re-size them correctly?

Basicly I want a max width of 300px;

my current code is

 $desired_width = 300;
 $desired_height = 300;

 $sth = mysql_query("SELECT photobase FROM userpictures WHERE id = '".$array[0]."'");

 while($r = mysql_fetch_assoc($sth)) {
      $blobcontents = $r["photobase"];

      $im = imagecreatefromstring($blobcontents);
      $new = imagecreatetruecolor($desired_width, $desired_height);

      $x = imagesx($im);
      $y = imagesy($im);

      imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);

      imagedestroy($im);

      header('Content-type: <span class="posthilit">image</span>/jpeg');

      imagejpeg($new, null, 85);
like image 990
RussellHarrower Avatar asked Dec 01 '25 13:12

RussellHarrower


1 Answers

A simple method to resize a image keeping the constraint proportions:

<?php
// Constraints
$max_width = 100;
$max_height = 100;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$width);
$height = intval($ratio*$height);
?>
like image 124
Vasil Dakov Avatar answered Dec 03 '25 02:12

Vasil Dakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!