List Info

Thread: dynamically generated images




dynamically generated images
user name
2007-04-19 13:14:03
I have put together a template based on some code found here
on this list.
It is designed to automatically create an image based on
some text using
GD::Simple. Unfortunately HTML/CSS doesn't allow for
landscape text, so one
must use images. The template generates the images, then
attaches it to the
$story as related media.

This template seems to work fine initially, then after
publishing the main
article 3 times it no longer works correctly. That is, the
images disappear
from the server (after publishing) as if they have been
expired. Looking at
the log for the image, I see that the image was expired and
then published.
I'm not sure why this is happening. Can someone give me some
pointers?

<%method verticalText>
> 	<%args>
> 	$text
> 	</%args>
>
> 	<%perl>
> 	use GD::Simple;
> 	use IO::Scalar;
>
> 	my $title = $story->get_title . ":
$text";
> 	my $user = Bric::Biz::Person::User->lookup({ login
=> 'admin' });
>
> 	# create the image
> 	my ($width, $height) = (30, 409);
> 	my $img = GD::Simple->new( $width, $height );
> 	$img->bgcolor( 238, 205, 43 );
> 	$img->fgcolor( 238, 205, 43 );
> 	$img->rectangle( 0, 0, $width, $height );
> 	$img->fgcolor('black');
> 	my ($twidth, $theight) = $img->stringBounds( $text
);
> 	my $x = int( $width/2 + $theight/2);
> 	my $y = int( $height/2 + $twidth );
> 	$img->font('helvetica');
> 	$img->fontsize(16);
> 	$img->angle(-90);
> 	$img->moveTo( $x, $y );
> 	$img->string( $text );
> 	my $imgFile = IO::Scalar->new( ($img->png) );
>
> 	# check for existing media
> 	my ($image, $subElement) = (undef, undef);
> 	for my $container ( $element->get_elements(
'related_media' ) ){
> 		my $i = $container->get_related_media;
> 		if( $i and lc $i->get_title eq lc $title){
> 			$subElement = $container;
> 			$image = $i;
> 			$image->checkout({ user__id =>
$user->get_id });
> 			last;
> 		}
> 	}
>
> 	# Create a new media item if necessary
> 	unless( $image ){
> 		my $assetType = Bric::Biz::ElementType->lookup({
'key_name' => 'layout_image' });
> 		$image = Bric::Biz::Asset::Business::Media->new({
> 			priority => $story->get_priority,
> 			user__id => $user->get_id,
> 			title => $title,
> 			category__id =>
$story->get_primary_category->get_id,
> 			site_id => $story->get_site_id,
> 			cover_date => $story->get_cover_date,
> 			element_type => $assetType,
> 			source__id => $story->get_source__id,
> 			cover_date => $story->get_cover_date
> 		});
> 	}
>
> 	# upload and save
> 	$text =~ s/s+/_/g;
> 	$image->upload_file( $imgFile, lc
"$text.png" );
> 	$image->checkin;
> 	$image->save;
>
> 	# publish the image
> 	$burner->blaze_another( $image );
>
> 	# add relationship to story
> 	unless( $subElement ){
> 		my $assetType = Bric::Biz::ElementType->lookup({
'key_name' => 'related_media' });
> 		$subElement = $element->add_container( $assetType
);
> 		$element->save;
> 		$subElement->set_related_media( $image );
> 		$subElement->save;
> 	}
> 	</%perl>
>
> 	<img src="<% $image->get_uri %>"
/>
> </%method>
>
> --
http://glassbrian.com
http://cooking.glassbri
an.com
Re: dynamically generated images
user name
2007-04-20 06:15:59
Hi Brian,

I'm not sure if your media issue may be related to the the
bug  
discussed here:
http://marc.info/?l=bricolage-general&m=1
17468956622539&w=2

Either way, I wonder if you've looked at SIFR for creating
nice  
looking story titles?
http://www.mikein
dustries.com/sifr/

Phillip.

On 19-Apr-07, at 2:14 PM, Brian Glass wrote:

> I have put together a template based on some code found
here on  
> this list.
> It is designed to automatically create an image based
on some text  
> using
> GD::Simple. Unfortunately HTML/CSS doesn't allow for
landscape  
> text, so one
> must use images. The template generates the images,
then attaches  
> it to the
> $story as related media.
>
> This template seems to work fine initially, then after
publishing  
> the main
> article 3 times it no longer works correctly. That is,
the images  
> disappear
> from the server (after publishing) as if they have been
expired.  
> Looking at
> the log for the image, I see that the image was expired
and then  
> published.
> I'm not sure why this is happening. Can someone give me
some pointers?
>
> <%method verticalText>
>> 	<%args>
>> 	$text
>> 	</%args>
>>
>> 	<%perl>
>> 	use GD::Simple;
>> 	use IO::Scalar;
>>
>> 	my $title = $story->get_title . ":
$text";
>> 	my $user = Bric::Biz::Person::User->lookup({
login => 'admin' });
>>
>> 	# create the image
>> 	my ($width, $height) = (30, 409);
>> 	my $img = GD::Simple->new( $width, $height );
>> 	$img->bgcolor( 238, 205, 43 );
>> 	$img->fgcolor( 238, 205, 43 );
>> 	$img->rectangle( 0, 0, $width, $height );
>> 	$img->fgcolor('black');
>> 	my ($twidth, $theight) = $img->stringBounds(
$text );
>> 	my $x = int( $width/2 + $theight/2);
>> 	my $y = int( $height/2 + $twidth );
>> 	$img->font('helvetica');
>> 	$img->fontsize(16);
>> 	$img->angle(-90);
>> 	$img->moveTo( $x, $y );
>> 	$img->string( $text );
>> 	my $imgFile = IO::Scalar->new( ($img->png)
);
>>
>> 	# check for existing media
>> 	my ($image, $subElement) = (undef, undef);
>> 	for my $container ( $element->get_elements(
'related_media' ) ){
>> 		my $i = $container->get_related_media;
>> 		if( $i and lc $i->get_title eq lc $title){
>> 			$subElement = $container;
>> 			$image = $i;
>> 			$image->checkout({ user__id =>
$user->get_id });
>> 			last;
>> 		}
>> 	}
>>
>> 	# Create a new media item if necessary
>> 	unless( $image ){
>> 		my $assetType =
Bric::Biz::ElementType->lookup({ 'key_name' =>  
>> 'layout_image' });
>> 		$image =
Bric::Biz::Asset::Business::Media->new({
>> 			priority => $story->get_priority,
>> 			user__id => $user->get_id,
>> 			title => $title,
>> 			category__id =>
$story->get_primary_category->get_id,
>> 			site_id => $story->get_site_id,
>> 			cover_date => $story->get_cover_date,
>> 			element_type => $assetType,
>> 			source__id => $story->get_source__id,
>> 			cover_date => $story->get_cover_date
>> 		});
>> 	}
>>
>> 	# upload and save
>> 	$text =~ s/s+/_/g;
>> 	$image->upload_file( $imgFile, lc
"$text.png" );
>> 	$image->checkin;
>> 	$image->save;
>>
>> 	# publish the image
>> 	$burner->blaze_another( $image );
>>
>> 	# add relationship to story
>> 	unless( $subElement ){
>> 		my $assetType =
Bric::Biz::ElementType->lookup({ 'key_name' =>  
>> 'related_media' });
>> 		$subElement = $element->add_container(
$assetType );
>> 		$element->save;
>> 		$subElement->set_related_media( $image );
>> 		$subElement->save;
>> 	}
>> 	</%perl>
>>
>> 	<img src="<% $image->get_uri
%>" />
>> </%method>
>>
>> --
> http://glassbrian.com
> http://cooking.glassbri
an.com

--
Phillip Smith,
Simplifier of Technology
Community Bandwidth



[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )