Jump to content

Page Image Manipulator | API for 1 & 2


horst

Recommended Posts

The next version of PiM will be able to enhance the Thumbnails module

(after I have successful send a pull request to apeisa and he have had time to check and implement the code, - hopefully)
 
 
While writing on the (optional) support for PiM with Thumbnails, owzim has written the js-code that rebuilds the crop rectangle, - and also he has tested and forthrightly commented my various tests with that. ^-^
 
Thumbnails will recognize if PiM is installed and use a different routine then. And without PiM the Thumbnails Module will use the PW core ImageSizer. Since dev-version 2.3.3 the ImageSizer uses automatic sharpening after resize. So - once after updating to the new module and a newer PW version (dev 2.3.3 or stable 2.4) everyone has sharpening support. :)

We both (owzim & me) want to have the coords & data stored and pre-selected when coming back to a previous created thumbnail. We use a wrapper class with a read and a write method on a session basis to achieve that. (After closing the browser or otherwise destroy the session, all data is lost)
 
But together with PiM the coords and options can be stored permanent! It can be stored as metadata in a IPTC custom field (2#215) with the original imagefile. :)

If you want enable permanent storage you have to create a config array named imageManipulatorOptions in your site/config.php and create the key thumbnailCoordsPermanent and set it to true:

$config->imageManipulatorOptions = array(
   // ...
	'thumbnailCoordsPermanent' => true
);

So, - if you don't want permanent storage, you are still able to use Thumbnails Module and PiM together, but without permanent storage.


 
https://youtu.be/IHwjL7YSfRo
 

EDIT: PullRequest is send https://github.com/apeisa/Thumbnails/pull/13 (the unorthodox way) :P

Edited by horst
  • Like 6
Link to comment
Share on other sites

@horst Thanks for the explanation. Maybe it's also ok if you mention the transparency range in your documentation so it's not necessary to change the code ;-)

I have had to rewrite it because the alpha transparency wasn't supported at all.

I just have pushed the version 0.0.8 to GitHub. :)

post-1041-0-15209900-1378060978_thumb.jp

There are some other fixes and changes with the version 0.0.8:

  • added enhanced support for Thumbnails module, including permanent storage for CropRectangleCoords and params
  • fixed / rewritten all bg-color stuff to support rgba alpha channel
  • fixed a E-Notice with IPTC prepare
  • changed the params of method resize, width, height to be the same like in new ImageSizer, ($sharpen can have value 'none')

I will also update the first post of this thread.

  • Like 4
Link to comment
Share on other sites

@Marty: thanks! Have you seen the post about PiM working together with Thumbnails too? I think you will like it! At least you are responsible for the existence of the Thumbnails Module. ^-^  ( and yes, - Antti  ^-^  is responsible too)

I'm very new and inexpertly with GitHub, because of that I have asked owzim if he can help me to send a pull request to Thumbnails repo with only code for sharpening support. While talking/writing about that GitHub stuff - we also come to conclusion that Thumbnails needs recreation of previously used thumbs. And as I don't know the JS stuff, owzim seems to know it very well and he wrote it! ^-^

I really like that community here and also how good people work together to make things good and better. ^-^

Thank you!

  • Like 4
Link to comment
Share on other sites

Hi Horst,

Trying to create a gif from a PNG 24. 

echo $img->name; // image.png

$info = $img->pimLoad("test", true)->getImageInfo();
var_dump($info);

// array(11) {
//   ["type"]=> string(3) "png"
//   ["imageType"]=> int(3)
//   ["mimetype"]=> string(9) "image/png"
//   ["width"]=> int(114)
//   ["height"]=> int(48)
//   ["landscape"]=> bool(true)
//   ["ratio"]=> float(2,375)
//   ["bits"]=> int(8)
//   ["channels"]=> int(6)
//   ["colspace"]=> string(9) "DeviceRGB"
//   ["alpha"]=> bool(true)
// }

$pim = $img->pimLoad("test", true)->pimSave();
Warning:  imagealphablending() expects parameter 1 to be resource, null given in /path/to/htdocs/site/modules/PageImageManipulator/ImageManipulator.class.php on line 684

$pim = $img->pimLoad("test", true)->setOutputFormat("gif")->pimSave();
Warning:  imagegif() expects parameter 1 to be resource, null given in /path/to/htdocs/site/modules/PageImageManipulator/ImageManipulator.class.php on line 681

$pim = $img->pimLoad("test")->setOptions(array("outputFormat" => "gif"))->pimSave();
Warning:  imagegif() expects parameter 1 to be resource, null given in /path/to/site/modules/PageImageManipulator/ImageManipulator.class.php on line 681
Link to comment
Share on other sites

Hi Martijn,

sorry, in a previous version this has worked, but by rewriting for rgba support I must have broken it. I have pushed an update to github that solved the issue.

But I think the result from converting transparent png to gif isn't what you expect :( 

Because transparency get not transformed into GIF-Palette-Color. It seems this isn't possible, - at least not that simple. ??

But bug is fixed now, thanks for your help!

Link to comment
Share on other sites

Hi Horst, 

TNX for your followup & bug fix.

<horst>But I think the result from converting transparent png to gif isn't what you expect :(</horst>

Is it possible to create a jpeg from the PNG 24 attach a background form a variable value [ rgb(100,100,100) ] then scale it and save it to gif ?

( want to use it in for newsletters )

Link to comment
Share on other sites

Hi Martijn,

can you only use gif? (Newsletter with embedded cid: ?)

You may try:

function pimPng2Gif($im1, $rgb) {
	imagealphablending($im1, true);
	imagesavealpha($im1, false);
	$w = imagesx($im1);
	$h = imagesy($im1);
	$im2 = imagecreatetruecolor($w, $h);
	imagealphablending($im2, true);
	imagesavealpha($im2, false);
	$bg = imagecolorallocate($im2, $rgb[0], $rgb[1], $rgb[2]);
	imagefilledrectangle($im2,0,0,$w-1,$h-1,$bg);
	imagecopy($im2,$im1,0,0,0,0,$w,$h);
	imagedestroy($im1);
	return $im2;
}

$rgb = array(219,17,116);

$png = $page->images->first()->pimLoad('png2gif',true);
$gif = $png->setMemoryImage( pimPng2Gif($png->getMemoryImage(), $rgb) )->setOutputformat('gif')->save();

echo "<img src='{$gif->url}' /> {$gif->name}";

Edited by horst
  • Like 1
Link to comment
Share on other sites

Hi guys, im totaly new to PHP and i cant figure out what im doing wrong here :

<ul>
	<?php
		$news = $pages->find("parent=1009");
		$out =" ";
		foreach($news as $news){
			$pageimage = $news->images->first->width(313)->pimLoad('news')->pimSave()->url;
			$out .= "<li><a href='{$news->url}'>";
			$out .= "<img src='{$pageimage}' alt='' />";
			$out .= "{$news->title}";
			$out .= "{$news->body}";
			$out .= date("j.m.Y", $news->created);
			$out .= "</a></li>";
		}
	echo $out;
 ?>
</ul>

and here is my error:
 

<p class="error WireFatalError">
Error: Call to a member function width() on a non-object (line 9 of D:\xampp\htdocs\projects\chemik\site\templates\home\news.html)
<br>
<br>
<em>This error message was shown because you are logged in as a Superuser. Error has been logged. </em>

Ty.
 

Link to comment
Share on other sites

foreach($news as $news){

On this line you are overriding the array news, by assigning the individual item to it. Change one of those variables to something different. Don't forget to change all occurrences of it.

  • Like 1
Link to comment
Share on other sites

@vxda:  and of course, for resizing you don't need the Pageimage Manipulator. It is enough to call ->width() on a pageimage:

// example
$pageimage = $myNewsItem->images->first->width(313)->url;

But also your variable names are a bit confusing: your variable $pageimage contains only the URL. I would change this to avoid confusion:

// example
$pageimage = $myNewsItem->images->first->width(313);

// and then
$out .= "<img src='{$pageimage->url}' alt='' />";

References on how to work with images:

Edited by horst
  • Like 1
Link to comment
Share on other sites

@horst:
Ty for a quick reply.

i changed my code to this

<?php
	$news = $pages->find("parent=1009");
	$out ="";
	foreach($news as $newsitem){
		$newsimg = $newsitem->images->first->width(313);
		$out .= "<li><a href='{$newsitem->url}'>";
		$out .= "<img src='{$newsimg->url}' alt='' />";
		$out .= "{$newsitem->title}";
		$out .= "{$newsitem->body}";
		$out .= date("j.m.Y", $newsitem->created);
		$out .= "</a></li>";
	}
	echo $out;
 ?>

and i still have this error.

when i remove

->width(313)

it picks the right picture and it all works, but without resizing img.

what am i missing ?

Thank you

Link to comment
Share on other sites

Ok i got it ;) problem solved

adding:

if ($newsimg) {
	$newsimg = $newsimg->size(313,177)->url;
}

solved the problem so my code looks like this now :

<?php
				$news = $pages->find("parent=1009");
				$out ="";
					foreach($news as $newsitem){
						$newsimg = $newsitem->images->first;
						if ($newsimg) {
							$newsimg = $newsimg->size(313,177)->url;
						}
						$out .= "<li><div><a href='{$newsitem->url}'>";
						$out .= "<img src='{$newsimg}' alt='' />";

						$out .= "{$newsitem->title}";
						$out .= "{$newsitem->body}";
						$out .= date("j.m.Y", $newsitem->created);
						$out .= "</a></div></li>";
					}
					echo $out;
				 ?>

Thank you for help.

  • Like 1
Link to comment
Share on other sites

vxda glad you figured it out.

It's always good to check if there's images, you could also use count(), and maybe also not output the image if there's no image.

So you could also do

...
$out .= "<li><div><a href='{$newsitem->url}'>";
if ($newsitem->images->count()) {
   $out .= "<img src='{$newsitem->images->first->size(313,177)->url}'/>";
}
$out .= "{$newsitem->title}";
...
  • Like 1
Link to comment
Share on other sites

Hi Horst,

thanks for this really impressive module. It's a dream (even the watermarking is important for me).

I have one problem.

I often have to delete images and replace with newer ones.

But then i get a large amount of orphaned pim-image-files in my page assets folders.

A solution was mentioned here:

http://processwire.com/talk/topic/4437-delete-orphaned-filesimages-from-siteassetsfiles/

But isn't there a way to delete the appropriate pim-image-file(s) automatically when deleting the original pw-image-file ?

Greetings,

Wolf

Link to comment
Share on other sites

... But isn't there a way to delete the appropriate pim-image-file(s) automatically when deleting the original pw-image-file ?

Actually you only can use the method removePimVariations() on a per image base to delete all your testfiles!

$img = $page->images->first();

$img->pimLoad('prefix')->removePimVariations();

and they all are gone.

But automaticly delete them when deleting a original image isn't supported yet. At least if you delete them manually in the backend. If you delete them via the API, there should be a simple way / workaround to get it solved. How do you delete them?

When deleteing images manually in the backend:

If you really have much changes and therefor a lot of orphaned files, you may use the lazycron module (core, but you have to install it first) together with the delete-orphaned-files script.

It would be much better to get rid of that automaticly as you said, but I need to find the way how to do it. And within next weeks there are no time available for extending PiM. :(

Edited by horst
Link to comment
Share on other sites

  • 3 months later...

Today I have added a hook to the module that add pim_* variations to pageimage-variation-collection.

This is usefull to not have mass orphaned (pim_)images on the disk when original images got deleted.

I have had this code ready for many weeks and would have done some more (workaround for GDlibs buggy sharpening on pngs with alpha-transparency) before update the module. But I haven't had time for this.

So, there are no other changes in module version 0.1.1 what is available now at github and in the modules section.

Cheers!

  • Like 3
Link to comment
Share on other sites

  • 4 months later...

Thank you, Horst, for this great module.

Bug report: the ratio isn't calculated correctly.

Two lines containing:

'ratio'	   => floatval(($info[0]>=$info[1] ? $info[0]/$info[1] : $info[1]/$info[2]))

have to be:

'ratio'	   => floatval(($info[0]>=$info[1] ? $info[0]/$info[1] : $info[1]/$info[0]))
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...