Current location: Hot Scripts Forums » Programming Languages » PHP » Stuck on imagejpeg whilst trying to upload BLOB to MySQL!


Stuck on imagejpeg whilst trying to upload BLOB to MySQL!

Reply
  #1 (permalink)  
Old 03-01-04, 10:58 PM
zcosgrove zcosgrove is offline
New Member
 
Join Date: Mar 2004
Location: Coffs Harbour, Australia
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Stuck on imagejpeg whilst trying to upload BLOB to MySQL!

Hi guys,

I am uploading a jpeg, resizing it, and then I want to upload it to mysql.

I am currently stuck at imagejpeg where I have resized the image, created it and now all I have to do is upload it to the db. I need the images to go into the
database as BLOB rather than the linking way of doing it.

The tricky bit is imagejpeg($new_image, '', $jpeg_quality);

What is the middle bit for? All I get is BLOB data outputted to the screen. I can't store it in a variable because then it comes out as a resource #, or a var/tmp/php whatever, all I want and need is the binary...

Here is my code:


PHP Code:

<?

if(!$submit){
?>
<html>
<title><head>Uploading, Resizing and Updating in MySQL</title></head>

<body bgcolor="white">
    <form method="post" action="new_index.php" enctype="multipart/form-data">
    <h1>Upload an Image File</h1> 
    <h3>Please fill in the details below to upload your file.</h3>
    <table>
    <col span="1" align="right">

    <tr>
       <td><font color="red">Name:</font></td>
       <td><input type="text" name="name" size=50></td>
    </tr>

    <tr>    
       <td><font color="red">File:</font></td>
       <td><input name="image" type="file"></td>
    </tr>

    <tr>
          <td><input type="hidden" name="MAX_FILE_SIZE" value="500000">
    <input type="submit" name="submit" value="Submit"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    
<?
// end if submit does not exist
else{


// intended thumbnail width + height
$img_width 100;
$img_height 75;

// set the jpeg quality
$jpg_quality 75;

trim($image);

$file_name $_FILES['image']['name'];
echo 
"<br><font color=blue><b>Filename: $file_name</b></font><br>";

$size GetImageSize($image);

$width $size[0];
$height $size[1];

$mime_type $_FILES['image']['type'];


if((
$mime_type == 'image/jpeg') || ($mime_type == 'image/pjpeg')){
echo 
"<br><font color=blue><b>Supported type uploaded</b></font><br>";
// end if unsupported mime_type has been uploaded
else{
echo 
"<br><font color=red><b>Unsupported MIME type uploaded. Please upload JPEG only</b></font><br>";
}


$img imagecreatefromjpeg($image);





// ------------------------- Get new sizes and ratio to change to --------------------
// NOTE: "keeping x to y ratio unchanged

// CREATE RATIO TO DOWNSIZE EXISTING IMAGE AND RETURN THE NEW DIMENSIONS
$width_ratio $img_width $width;
$height_ratio $img_height $height;

// ADJUST PORTRAIT IMAGES
if(($width <= $img_width) && ($height <= $img_height)){
         
         
$new_width $width;
         
$new_height $height;

// end if width is less than or equal to Max height and height is less than or equal to Max height

elseif($width $height){ // PORTRAIT IMAGE
         
         
$new_width $img_width;
         
$new_height ceil($width_ratio $height);

// end if height is greater than width

elseif($height $width){ // LANDSCAPE IMAGE
         
         
$new_height $img_height;
         
$new_width ceil($height_ratio $width);

// end if height ratio times width is less than Max width

// ------------------------- End getsizes --------------------



// ------------------------- Create re-sized blank image for resize -----------------

if(($new_height) && ($new_width)){
echo 
"<br><font color=blue><b>New height + width successful</b></font><br>";
// end if new height exist

$img_resized imagecreatetruecolor($new_width$new_height) or die ("<br><font color=red><b>Error: imagecreatetruecolor</b></font><br>");

if(
$img_resized){
echo 
"<br><font color=blue><b>Created resized template image</b></font><br>";
}

imagecopyresampled($img_resized$img0,0,0,0$new_width$new_height$width$height) or die ("<br><font color=red><b>Error: imagecopyresampled</b></font><br>");

// ------------------------- End re-size template --------------------





// ------------------------- Create new, original filename -----------------

$file_name time()."_$file_name";

if(
$file_name){
echo 
"<br><font color=blue><b>Filename: $file_name</b></font><br>";
// end if filename exists
else{
echo 
"<br><font color=red><b>Filename creation unsuccessful</b></font><br>";
}
// ------------------------- End new filename creation  --------------------



// ------------------------- Combine everything and create the image -----------------

imagejpeg($img_resized$image$jpg_quality);


// ------------------------- End create new, resized image  --------------------


// end else submit has been hit and the form has been processed

?>



Any suggestions? Other than use a link to the file on the server?

Last edited by mab; 10-04-07 at 12:19 PM. Reason: an oldie, but has code tags now...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-04-07, 12:06 PM
stephen.bigelis stephen.bigelis is offline
New Member
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ob_start

imagejpeg() likes to dump its results directly into the output stream unless a file name is provided. A quick solutions would be...

PHP Code:

ob_start(); // Start the output buffer. 

imagejpeg($new_image,NULL,$quality);
$new_image_data ob_get_contents(); 
ob_end_clean();
(!get_magic_quotes_gpc()) $new_image_data addslashes($new_image_data); 
I know it has been awhile since the original post, but I hate unanswered question appearing in my Google searches.

Cheers
_Stephen

Last edited by mab; 10-04-07 at 12:21 PM. Reason: Please use code tags when posting code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-04-07, 12:24 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Edit: Never mind what I originally posted here...

Is that susposed to be an "if" statement at the start of the last line of code?
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 10-04-07 at 12:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-04-07, 07:18 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Can I ask why you're storing images in the database itself? It's a fairly inefficient way to do it; the usual way is to keep the images in a directory and just store pointers (paths) to the images in the database. The overhead required for storing and retrieving images from a database is pretty high compared to just storing the pointers.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-09-07, 04:44 PM
stephen.bigelis stephen.bigelis is offline
New Member
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
yes .. an if .. sorry for the delay

On the other note... Storing the images in database. The images I am storing are photos of users. They are resized before storing, so they are not that large. The benefits for me are: A. Ease B. Backed up with the rest of the data. C. I can easily send them through standard XML to other applications. This is definitely a much debated topic though. As I understand it, when you are talking about very large systems it might actually be better to keep it in a database because you more easily replicate the databases. Anyway, I haven't yet noticed a slowdown from it.

One point of note for beginners: Try not to store images and large text blocks in the same table as other data. For me I have user data in one table and I keep their images separately in a two column InnoDB table (image_id and Blob). Which a resource table (user_id, image_id) can then keep track of the instances. When your SQL is set up right with references and cascades, it can manage them for you. (as to delete images when you delete users etc.) This way when I access my user data and I am not accessing the images.

Last edited by stephen.bigelis; 11-09-07 at 04:50 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-30-10, 12:03 PM
krabats krabats is offline
New Member
 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
X_X

ob_start();
echo imagejpeg($dst_r,null,$jpeg_quality);
$image = ob_get_clean();
ob_end_clean();
$image = addslashes($image);

This is how it worked for me...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
Upload image & text to MYSQL --> AT THE SAME TIME? charliedhq PHP 1 02-20-04 03:57 PM
Basic image upload and data to mysql mdhall Script Requests 3 11-05-03 06:00 PM
File upload and path to mysql dalio PHP 0 08-12-03 09:24 AM
upload local mysql to internet borgo PHP 1 06-26-03 01:04 AM


All times are GMT -5. The time now is 01:32 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.