Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+4 votes
3.1k views
in Q2A Core by
edited by
I am still trying to get thumbnails or resized images to avoid very slow loadingtimes for firt time visitors of my pages.

Timthumb, phpthumb, slir all do a good job but not with blobimages as source !

Any hint is welcome.
Q2A version: 1.5.2

2 Answers

+1 vote
by
Well actually I would like to hear a solution to this issue as well, I also wanted to use timthumb once, but this fails because of the blob URLs.
by
edited by
@Aslan, ia m close to have that resolved.
Actually it is possible to save at least the image to filesystem, so one can use timthumb to generate thumbnails.

To do so install timthumbs, copying the php file to root.
Add the cache folder under root.
Add a myimages folder under root.

Next open
qa-db-blobs.php in include folder, go to line 49

            qa_db_query_sub(
                'INSERT INTO ^blobs (blobid, format, content, filename, userid, cookieid, createip, created) VALUES (#, $, $, $, $, #, INET_ATON($), NOW())',
                $blobid, $format, $content, $filename, $userid, $cookieid, $ip
            );

Add after that the code:

$im = imagecreatefromstring($content);
if ($im !== false) {
//header('Content-Type: image/jpeg');
imagejpeg($im, 'myimages/'.$blobid.'.jpg');
imagedestroy($im);
}
else {
echo 'An error occurred.';
}

After that follows again the normal code

            return $blobid;
        }


Now each uploaded image is stored in the myimages folder as jpg with blobid as name.

Now You can refer through timthumb to that image setting the size You want.

It is not much but it works. As long as there is no real plugin available I will give it a try. I wanted clientside resizing, but I think there is no major interest now.

The advantage is, that images are still stored in the database so if a plugin is done, You still have any information in place.

You can call the thumbs like:

http://www.mypage.com/timthumb.php?src=blobid.jpg&w=200

to have a 200px width proportional image.

Timthumb was Your idea, thank You so far.

To retrieve the blobids for Your thumbs You must go through the question list and serch the blobids, later more on that.

I am working on that, and I hope i can make it still better, but  am in need of input by pro´s...

EDIT:
@Aslan, I think I have found how to resize the image nicely so You can store directly thumbnails into a folder. So no need to use timthumbs.

Will have that ready tomorrow...
by
Dude, you are awesome! I'll give this a try in a few days and let you know, sounds very promising.
by
Thank You, but after two weeks it was time to get something working..thanks to google in first place...
I have added the second answer, You can set a wanted width for the pic stored in filesystem under blobid-600.jpg and than use it directly if You know the blobid or using timthumb to do whatever You want...
All code is comented so it should be easy to change it to your needs.
+1 vote
by
edited by

In the following chapter I have put together what I could find. I am not a programmer and any help on this code is very welcome!!! Especially error handling and memory optimization is needed. 

Best would be if some pro could put that all together into a plugin.

It works fine on php with gd library installed, what should be the case at 99% of hosters.

The code gives us the following possibilities:

- Storing copy of original image in filesystem, using the origianl blobid to find it later.

- Storing resized image or thumbnail in filesystem.

- Using resized image for blob in database to have faster loading pages, especially for those who visit sites with many large images first time.

- Having smaller databases if images are used a lot.

Instruction

Save a copy of ../qa-inlude/qa-db-blobs.php to replace it again if needed

Open ../qa-inlude/qa-db-blobs.php

Replace the function qa_db_blob_create (..) { ............ }

with:

 

function qa_db_blob_create($content, $format, $filename=null, $userid=null, $cookieid=null, $ip=null)
/*
Create a new blob in the database with $content and $format, returning its blobid
*/
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
 
for ($attempt=0; $attempt<10; $attempt++) {
$blobid=qa_db_random_bigint();
 
if (qa_db_blob_exists($blobid))
continue;
 
// FROM HERE INSERTED CODE TO STORE ORIGINAL IMAGE IN FILESYSTEM 
// AND SMALL IMAGE IN FILESYSTEM 
// AND USE SMALL IMAGE FOR DATABASE BLOB
// WE MUST ADD A FOLDER myimages TO ROOT
// IF YOU PREFER ANOTHER NAME CHANGE AS WELL myimages IN THE FOLLOWING CODE
 
$im = imagecreatefromstring($content); // create image from string
if ($im !== false) {
//header('Content-Type: image/jpeg');
imagejpeg($im, 'myimages/'.$blobid.'.jpg'); // save original image
 
$width = imageSX($im); // get old width
$height = imageSY($im); // get old height
$aspect_ratio = $width/$height; // get ratio
$wantedwidth = 600; // set wanted width --- IF You change wantedwidth as well change -600 in line 63 AND line 75
$neededheight = $wantedwidth / $aspect_ratio; // calculate needed height
$newimage = imagecreatetruecolor($wantedwidth, $neededheight); // build new truecolor image with new width and height
imagecopyresampled($newimage, $im, 0, 0, 0, 0, $wantedwidth, $neededheight, $width, $height); // copy resized original into new image
imagejpeg($newimage, 'myimages/'.$blobid.'-600.jpg'); // save small image --- change 600 to wantedwidth
 
imagedestroy($im);
imagedestroy($newimage);
 
}
else {
echo 'An error occurred.';
}
 
//Here replacing content variable for blob creation with small image
//I think we should replace the variable $format as well to jpeg but i am not sure about that
//IF WE DONT WANT TO SAVE THE SMALL IMAGE BUT THE ORIGINAL TO DATABASE DELTE THE FOLLOWING LINE
$content=file_get_contents('myimages/'.$blobid.'-600.jpg'); // replace original content with small image string --- change 600 to wantedwidth
 
// END OF INSERTED CODE
 
qa_db_query_sub(
'INSERT INTO ^blobs (blobid, format, content, filename, userid, cookieid, createip, created) VALUES (#, $, $, $, $, #, INET_ATON($), NOW())',
$blobid, $format, $content, $filename, $userid, $cookieid, $ip
);
 
return $blobid;
}
 
return null;
}
 
 
Thats all. Please help on rewriting or optimizing that code.
 
Open problems:
Should the format variable beeing rewritten if changing $content to save new image as blob?
How to make sure that blobid refers to a image if pfd upload as well is activated ?
Best way to get a posts first blobid to build a link to the image stored in filesystem ?
 
Edit: If You want that images which are smaller then the wanted width are not expanded to 600 px width, add a if {} checking if the original width is smaller than the wanted width.
 

ADDED: CODE TO FIND BLOBID IN POST NEEDED TO BUILD AND CALL STORED IMAGE

This is the code I use to find the first blobid of a post in advanced theme functions. In my case it works but..

It needs to be rewritten ! Please help on that as well !

 

      if (isset($this->content['q_view']['raw']['content'])) {
      $mycontent=$this->content['q_view']['raw']['content'];
      $findme   = 'blobid';
      $pos = strpos($mycontent, $findme);
      if ($pos === false) {
      } else 
 {
$pieces = explode("blobid=", $mycontent);
$myblob=$pieces[1];
 
$pieces2 = explode("\" style", $myblob);
 
$pieces2 = explode("\" width", $pieces2[0]);
$pieces2 = explode("\"", $pieces2[0]);
$finalblobid=$pieces2[0];
//print $finalblobid;
   }
      
      }else{
      }
 
There is as well a fuction in q2a which may be used to get the posts blob id
 
qa_get_blob_url($blobid, $absolute) returns a URL which can be used to view or download $blobid. If $absolute is false, this URL will be relative to the current Q2A page being requested, otherwise the URL will be absolute.
 
But I dont know how to use it..
by
Hi, this looks useful. Have you made any further updates to this code?
...