Jump to content

Resizing a blob


awebbdesigner

Recommended Posts

Hi I have create a script that adds a image as blob successfully,

however now I want to create a thumbnail, I have the following code; can someone help...

 

# open and code into blob

$fp = fopen($safename, 'r');

$content = fread($fp, filesize($safename));

$thumb = $content;

$content = addslashes($content);

fclose($fp);

 

# resize accordingly...

$thumb = new resize($content, $width, $height, 300);

 

# the class that does the resizing (WHERE I THINK ITS GONE WRONG)

class resize {

public $new_image_blob = "";

function __construct($blob, $width, $height, $amount) {

 

  # the maximum width and height as set by the user

  $thumb_height_max = $amount;

  $thumb_width_max = $amount;

 

  # maintain aspect ratio landscape or portrait

  if($width < $height) {

  $new_width = ($thumb_height_max / $height) * $width;

  $new_height = $thumb_height_max;

  $needtoresize = ($height < $thumb_height_max);

  } else {

  $new_width = $thumb_width_max;

  $new_height = ($thumb_width_max / $width) * $height;

  $needtoresize = ($width < $thumb_width_max);

  }   

 

  # now that we have the new width and heightwe need to resize the blob

  $im = imagecreatefromstring($blob);

  $thumb = imagecreatetruecolor($new_width, $new_height);

  imagecopyresampled($thumb, $im, 0, 0, 0, 0, $new_width, $new_height, ImageSX($im), ImageSY($im));

  $this->new_image_blob = addslashes($thumb);

}

}

 

# then the query below adds the code (the original blob goes in correctly (ablob) but bblob (the resized blob doesn't))

$iS = "INSERT INTO $tableb (pal, afield, bfield, cfield, dfield, efield, ffield, gfield, ablob, bblob, cblob, dblob) VALUES

('6', '$fk', '$filename', '$size', '$fileExtension', '$width', '$height', '$orientation', '$content', '$thumb->new_image_blob', '$four', '$two')";

 

Link to comment
Share on other sites

Found the solution, needed to re-write my resize class to work off the temporary image upload and not the blob...

 

The upload processing page

<?php

 

ini_set('display_errors', 1);

error_reporting(E_ALL);

 

include("../../connect.php");

include("resize.php");

 

# these settings should help

set_time_limit(0);

ini_set('upload_max_filesize', '12M');

ini_set("memory_limit", "16M");

 

# going in as a blob from now on

$stamp = mktime();

$safename = $_FILES['Filedata']['tmp_name'];

$filename = $_FILES['Filedata']['name'];

$size = $_FILES['Filedata']['size'];

$type = $_FILES['Filedata']['type'];

$fk = $_REQUEST['fk'];

 

# open and code into blob

$fp = fopen($safename, 'r');

$content = fread($fp, filesize($safename));

$thumb = $content;

$content = addslashes($content);

fclose($fp);

 

# find out what it is, uploadify send everything through as application, so we need to split the filename

$fileExplode = explode('.', $filename, 3);

$fileExtension = strtolower($fileExplode[1]);

 

if($fileExtension == 'jpg' xor $fileExtension == 'png' xor $fileExtension == 'gif') {

 

# find out image size and create thumbnails... 800 400 100

list($width, $height) = getimagesize($safename);

if ($width > $height) { $orientation = "landscape";

} elseif ($width < $height) { $orientation = "portrait";

} else { $orientation = "square"; };

 

# max width/height = 800

$eight = new resize($safename, $width, $height, 800);

$four = new resize($safename, $width, $height, 400);

$one = new resize($safename, $width, $height, 150);

 

}

 

$iS = "INSERT INTO $tableb (pal, afield, bfield, cfield, dfield, efield, ffield, gfield, ablob, bblob, cblob, dblob) VALUES

('6', '$fk', '$filename', '$size', '$fileExtension', '$width', '$height', '$orientation', '$content', '$eight->new_image_blob', '$four->new_image_blob', '$one->new_image_blob')";

$iQ = mysql_query($iS) or die (mysql_error());

 

print "1";

 

// print "<p>$eight->new_image_blob</p>";

 

?>

 

The class I wrote to solve resize the images into blobs...

 

<?php

 

class resize {

public $new_image_blob = "--";

function __construct($image_file, $width, $height, $amount) {

 

        # the maximum width and height as set by the user

        $thumb_height_max = $amount;

                $thumb_width_max = $amount;

 

# maintain aspect ratio landscape or portrait

if($width < $height) {

$new_width = ($thumb_height_max / $height) * $width;

                    $new_height = $thumb_height_max;

$needtoresize = ($height < $thumb_height_max);

} else {

          $new_width = $thumb_width_max;

                    $new_height = ($thumb_width_max / $width) * $height;

                    $needtoresize = ($width < $thumb_width_max);

                }

   

        $src = imagecreatefromjpeg($image_file);

        $tmp = imagecreatetruecolor($new_width,$new_height);

        imagecopyresampled($tmp,$src,0,0,0,0,$new_width, $new_height,$width,$height);

   

        # get the image, then re-convert

        ob_start();

        imagejpeg($tmp, NULL, 100);

        $final_image = ob_get_contents();

        ob_end_clean();

 

  # this should be it

        $this->new_image_blob = addslashes($final_image);

  }

}

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.