Which is the purpose of this thread...
I have no idea how to write a thumbnail using the VB image class. According to
vB_Image_Abstract fetch_thumbnail creates the actual thumbnail, but I don't know what to do next. The VB upload class is even more confusing than the VB image class; and neither are well documented.
Let me explain my function... It takes in 2 variables, the first one is a URL to an image; the second is the ID where it will be stored.
Code:
function build_thumbnail($thumbnail, $mediaID)
{
global $vbulletin;
require_once(DIR . '/includes/class_vurl.php');
$vurl = new vB_vURL($vbulletin);
$vurl->set_option(VURL_URL, $thumbnail);
$vurl->set_option(VURL_RETURNTRANSFER, 1);
$vurl->set_option(VURL_FOLLOWLOCATION, 1);
$vurl->set_option(VURL_TIMEOUT, 30);
$result = $vurl->exec();
$target = DIR.'/'.$vbulletin->options['media_thumb_dir'].'/thumbs/'.$mediaID.'.jpg';
$file = fopen($target, 'wb');
fwrite($file, $result);
fclose($file);
preg_match('#(.*/)(.*)$#i',$target,$matches);
$location = $matches[1];
$filename = $matches[2];
$newRatio = 90 / 160;
$newWidth = 160;
$newHeight = $newRatio * $newWidth;
$quality = $vbulletin->options['media_thumb_quality'];
require_once(DIR . '/includes/class_image.php');
$image =& vB_Image::fetch_library($vbulletin);
$thumb = $image->fetch_thumbnail($filename, $location, $newWidth, $newHeight, $quality);
print_r($thumb);
exit;
}
As you can see, its pretty simple.
first it gets the image information using VB's built in cURL
then it uses fwrite to write the $result to $target
then it gets the location and filename of the file it just wrote to edit it
then it defines crop variables, width, height and quality
Then it should generate a thumbnail using VB's image class...
However, print_r($thumb) outputs the following:
Code:
Array ( [filedata] => [filesize] => 0 [dateline] => 0 [imageerror] => )
Obviously something is getting lost.
--------------- Added [DATE]1279646313[/DATE] at [TIME]1279646313[/TIME] ---------------
However, I don't think it even matters, since the VB image class doesn't crop anything, which is essential for this. I should just use the two methods for GD and Imagick; but I can't get the Imagick function working.
Code:
function build_thumbnail($thumbnail, $mediaID)
{
global $vbulletin;
require_once(DIR . '/includes/class_vurl.php');
$vurl = new vB_vURL($vbulletin);
$vurl->set_option(VURL_URL, $thumbnail);
$vurl->set_option(VURL_RETURNTRANSFER, 1);
$vurl->set_option(VURL_FOLLOWLOCATION, 1);
$vurl->set_option(VURL_TIMEOUT, 30);
$result = $vurl->exec();
$target = DIR.'/'.$vbulletin->options['media_thumb_dir'].'/thumbs/'.$mediaID.'.jpg';
$file = fopen($target, 'wb');
fwrite($file, $result);
fclose($file);
$newRatio = 90 / 160;
$newWidth = 160;
$newHeight = $newRatio * $newWidth;
$quality = $vbulletin->options['media_thumb_quality'];
if ($vbulletin->options['imagetype'] == 'GD')
{
list($oldWidth, $oldHeight) = getimagesize($target);
$oldRatio = $oldHeight / $oldWidth;
$tmpHeight = $oldRatio * $newWidth;
$offHeight = ($oldHeight - ($newRatio * $oldWidth)) / 2;
preg_match('#([\w]+)$#i',$thumbnail,$filetype);
switch ($filetype[1])
{
case "jpg": $oldImage = imagecreatefromjpeg($target); break;
case "png": $oldImage = imagecreatefrompng($target); break;
case "gif": $oldImage = imagecreatefromgif($target); break;
}
$newImage = imagecreatetruecolor(160, 90);
imagecopyresampled($newImage, $oldImage, 0, 0, 0, $offHeight, $newWidth, $tmpHeight, $oldWidth, $oldHeight);
imagejpeg($newImage, $target, $quality);
}
if ($vbulletin->options['imagetype'] == 'Magick')
{
$thumb = new Imagick($target);
$thumb->cropThumbnailImage($newWidth,$newHeight);
$thumb->writeImage($target);
$thumb->destroy();
}
}
The GD2 function works GREAT... the Imagick function breaks with a white screen.