RussellHarrower
RussellHarrower

Reputation: 6820

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);

Upvotes: 3

Views: 2239

Answers (1)

Vasil Dakov
Vasil Dakov

Reputation: 2039

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);
?>

Upvotes: 3

Related Questions