View Single Post
  #9  
Old 07-20-2010, 03:19 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01207 seconds
  • Memory Usage 1,786KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete