PDA

View Full Version : How does the Image Class Work?


Jaxel
05-06-2010, 11:41 PM
So I want to take an image... which is 4:3... resize it, and then crop it to 16:9. I know how to do this using GD2, but sadly it doesnt work with ImageMagick. So now I'm trying to use the existing image classes in VB to handle it... but I can't get them working either...
require_once(DIR . '/includes/class_image.php');
$image =& vB_Image::fetch_library();
$image->fetch_thumbnail($filename, $location, $newWidth, $newHeight, $quality);

Doesnt seem to do anything...

--------------- Added 1273196508 at 1273196508 ---------------

This is what I am using now...

if ($vbulletin->options['imagetype'] == 'GD')
{
$newImage = imagecreatetruecolor(160, 90);
$oldImage = imagecreatefromjpeg($target);
imagecopyresampled($newImage, $oldImage, 0, 0, 0, $offHeight, $newWidth, $newHeight, $oldWidth, $oldHeight);
unlink($target);
imagejpeg($newImage, $target, $quality);
}

/*
if ($vbulletin->options['imagetype'] == 'Magick')
{
$thumb = new Imagick($target);
$thumb->cropThumbnailImage($newWidth,$newHeight);
$thumb->writeImage($target);
$thumb->destroy();
}
*/


The second set is commented out, because I can't get it working... Either I need to figure out how to do this with the built in image classes... or I need to figure out how to do this with the imagick class... I can't figure out either.

Jaxel
05-09-2010, 03:49 AM
So anyone got any ideas?

Jaxel
05-12-2010, 09:00 PM
So absolutely no one knows how the image class works?

All I want to do is take an image, crop from center and resize.

Jaxel
05-17-2010, 08:52 PM
So yeah... bumping once again... Need to either get the image class working, or figure out how to resize an image using ImageMagick...

Jaxel
07-19-2010, 02:52 PM
Bump..........

Guest190829
07-19-2010, 02:59 PM
What do you mean it's not working? Are you getting errors?

Jaxel
07-19-2010, 07:50 PM
Which part are you talking about? The ImageMagick code doesn't work as it just sends me to a blank white page (stops processing code) and doesn't write any image to the $target file. The Image Class code doesn't work because its obviously incomplete and there isn't even a write step anywhere in the code. So I need to either find out why the ImageMagick code doesn't work, or complete the image class code. But the VB4 image class is obfuscated to all hell, so who knows what the hell is going on in those files.

Guest190829
07-19-2010, 08:27 PM
What is the returned value of $image->fetch_thumbnail() ?

Also, I'm not very familiar with vB's image class, but from the looks of it, it doesn't handle actually uploading the file. This is a good thing. You'll probably have to use vB's file class to upload it to your server.

Jaxel
07-20-2010, 03:19 PM
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 (http://members.vbulletin.com/api/vBulletin/vB_Image_Abstract.html) 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.

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:

Array ( [filedata] => [filesize] => 0 => 0 [imageerror] => )

Obviously something is getting lost.

--------------- Added [DATE]1279646313 at 1279646313 ---------------

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.

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.

Guest190829
07-20-2010, 05:02 PM
You have Image Magick installed on your server? (Let's get the easy questions out of the way first hehe)

Jaxel
07-20-2010, 07:13 PM
Haha... yes of course, the server I am trying to install it on does not have GD, it only has Image Magick.