I’m using the script from jquery file upload and modifying the create_scaled_image function to work in conjunction with my DB. Looks like the image is getting re-sized but the rsized image is completely black. Any idea what would cause this?
Here are my modifications to the script:
private function create_scaled_image($file_name, $options) {
list($img_width, $img_height) = @getimagesize($file_name);
error_log("Width: $img_width");
error_log("Height: $img_height");
if (!$img_width || !$img_height) {
return false;
}
$scale = min(
$options['max_width'] / $img_width,
$options['max_height'] / $img_height
);
error_log("SCALE: $scale");
if ($scale > 1) {
$scale = 1;
}
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = @imagecreatetruecolor($new_width, $new_height);
switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'jpg':
case 'jpeg':
$src_img = @imagecreatefromjpeg($file_name);
break;
case 'gif':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
$src_img = @imagecreatefromgif($file_name);
break;
case 'png':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
@imagealphablending($new_img, false);
@imagesavealpha($new_img, true);
$src_img = @imagecreatefrompng($file_name);
break;
default:
$src_img = $image_method = null;
}
@imagecopyresampled(
$new_img,
$src_img,
0, 0, 0, 0,
$new_width,
$new_height,
$img_width,
$img_height
);
// Free up memory (imagedestroy does not delete files):
@imagedestroy($src_img);
return $new_img;
}
My call to the function looks like this:
$options['max_width'] = 80;
$options['max_height'] = 80;
$thumb_source = $this->create_scaled_image($uploaded_file, $options);
ob_start(); // Start capturing stdout.
imagejpeg($thumb_source); // As though output to browser.
$thumb_bin = mysql_real_escape_string(ob_get_contents()); // the raw jpeg image data.
ob_end_clean(); // Dump the stdout so it does not screw other output.
// Code to insert data into my DB here
Remove the @’s in the code and look for the errors (if any). @ supress any error occuring while using the function behind.