Jump to content

Recommended Posts

I have images stored in a mysql table as binary and i reproduce them with this script:

<?php
require_once("functions.php");
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
	connectSQL();
        // get the image from the db
        $sql = "SELECT image FROM Images WHERE ImageID=".$_GET['image_id'];
         // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
         // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
         // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

 

My question is how can i resize that (i.e make it the image resource for GD) thats my own issue is figuring out how to link it as a resource in gd if its a temp image.

Link to comment
https://forums.phpfreaks.com/topic/64996-resizing-an-image-off-a-database/
Share on other sites

Here's the code I use to resize an image by scale:

 

<?php
    public function ResizeByScale($scale, $outputFile) {
                /**
                 * @param float scale: The scale at which you want to resize the image
                 * @return: void
                 * 
                 * @output: Writes $outputFile to folder.
                 * 
                 */   

                $width     = $this->getWidth();
                $height    = $this->getHeight();
                $newWidth  = $width * $scale;
                $newHeight = $height * $scale;
        $image_p   = ImageCreateTrueColor($newWidth, $newHeight);
        $image     = ImageCreateFromJPEG($this->getPath());

        ImageCopyResampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
        imagePNG($image_p, $outputFile, $this->getImageQuality());
?>

 

The thing you'll obviously have to modify is $width, $height, and the ImageCreateFromJPEG($this->getPath) <--- you can use your binary data for this I believe.

I found this on php.net and i think it will suite my needs just fine

<?php
    $data = @mysql_result($result,0,"image");
    $type = @mysql_result($result,0,"filetype");
   
    Header( "Content-type: $type");   
   
    $size = 150;  // new image width
    $src = imagecreatefromstring($data);
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;

    if ($width <= $size) {
      $new_w = $width;
      $new_h = $height;
    } else {
      $new_w = $size;
      $new_h = abs($new_w * $aspect_ratio);
    }

    $img = imagecreatetruecolor($new_w,$new_h);
    imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);

    // determine image type and send it to the client   
    if ($type == "image/pjpeg") {   
      imagejpeg($img);
    } else if ($type == "image/x-png") {
      imagepng($img);
    } else if ($type == "image/gif") {
      imagegif($img);
    }
    imagedestroy($img);
    mysql_close($link);
};
?>

I've got this version working now, but I want to know how can I resize based on  height?

<?php
require_once("functions.php");
$id = $_GET['image_id'];
if(!empty($_GET['width'])){
$size = $_GET['width'];
}
else{
$size = 500;
}
if($id) {
connectSQL();
    $query = "select Image, ImageType from Images where ImageID = $id";
    $result = mysql_query($query) or die(mysql_error());
    $data = mysql_result($result,0,"Image");
    $type = mysql_result($result,0,"ImageType");
$type = "image/".$type;
    Header("Content-type: ".$type);   
    $src = imagecreatefromstring($data);
    $width = imagesx($src);
    $height = imagesy($src);
    $aspect_ratio = $height/$width;

    $new_w = $size;
    $new_h = abs($new_w * $aspect_ratio);


    $img = imagecreatetruecolor($new_w,$new_h);
    imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
     // determine image type and send it to the client   
    if ($type == "image/pjpeg" || $type == "image/jpg" || type == "image/jpeg") {   
      imagejpeg($img);
    } else if ($type == "image/x-png" || $type = "image/png") {
      imagepng($img);
    } else if ($type == "image/gif") {
      imagegif($img);
    }
    imagedestroy($img);
}
?>

yeah, but I'm thinking its a bad idea because browsers have a set width (I don't like side scrolls) and an infinte  height so i'll just size in the height and if it doesn't fit my height needs I'll say scale it down more using a loop till i get numbers that manage. The width will be less than the desired, but it will suite my needs.

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.