vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How does the Image Class Work? (https://vborg.vbsupport.ru/showthread.php?t=242006)

Jaxel 05-06-2010 11:41 PM

How does the Image Class Work?
 
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...
Code:

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 [DATE]1273196508[/DATE] at [TIME]1273196508[/TIME] ---------------

This is what I am using now...

Code:

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

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.


All times are GMT. The time now is 05:49 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01003 seconds
  • Memory Usage 1,749KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (11)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete