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..