21282 Posts in 5730 Topics by 2601 members
General questions about getting started with SilverStripe that don't fit in any of the categories above.
|Go to End|
1 February 2011 at 2:57am
I'm finishing up a project at the moment and one minor to-do is left - visitors on the website can generate and save PDF files for instances of a DataObject class on the website, which have images linked to them. The PDF displays the DataObject's main image in it, on top of a polaroid background sprite.
In order to place the image exactly on top of the polaroid part of the background, it is first resized depending on its orientation (orientation 0 = SetSize, 1 = SetWidth, 2 = SetHeight). This keeps the aspect ratio of the image intact while making sure that at least one dimension of the image fits inside the polaroid. Unless the image is square (very rare), it still overflows one of the dimensions though.
Now we can just display the image at the maximum width and height allowed within the polaroid and we'd be done, but this defeats the purpose of resizing it in the first place because it would still end up skewed. So after the first resize (see previous paragraph), I've added in another line whcih uses the CroppedImage method of the Image class, to just "cut off" any part of the image which doesn't fit inside the polaroid. This way, most of the image is still showed and it doesn't become skewed. On the frontend some CSS (overflow: hidden) would suffice, but this doesn't seem to be possible in FPDF (the PDF extension we're using), which is why I'm trying to achieve the cropping this way.
The problem is, SilverStripe only seems to let me do one or the other - SetSize/-Width/-Height OR CroppedImage. If I try to consecutively execute both methods, the object variable turns into NULL, rendering it useless. I'm guessing this is because of all of these methods turning the Image object into an instance of Image_Cached? Does anyone have any ideas on this?
I've tried creating a new instance of Image and storing the resized image into it, then cropping that new instance. But it didn't get me anywhere. I can't use the GD2 methods because I don't think the server supports them; "GD Support" is "enabled" in phpinfo() but I get the "the method 'croppedresize' does not exist on 'Image'" error when I try to use one of the methods. SilverStripe version is 2.4.3.
Sorry for the long post (I have a habit of being long-winded), I hope it's clear what the problem is.
1 February 2011 at 1:24pm
I think the issue is because when you create a new thumbnail SilverStripe does not return that thumbnail as a GD / Image object so you can't therefore chain calls.
"the method 'croppedresize' does not exist on 'Image'" error when I try to use one of the methods.
No you are getting that error because that method doesn't exist. It is named 'generateCroppedImage()' in Image.php
1 February 2011 at 9:50pm Last edited: 1 February 2011 9:51pm
Thanks for your reply, Willr.
I'm with you on the first part of your reply, I can understand that SilverStripe returns an instance of Image_Cached after you manipulate an Image with (e.g.) the SetWidth() method and therefore revokes your access to the Image methods.
But about that error - it is odd, but I receive exactly the same error when I try to use the generateCroppedImage() method as well. As a matter of fact, calling the croppedResize() method also gives me the exact same error.
I know that the generatedCroppedImage() method expects a GD instance as one of its arguments, so I've tried to use the method in the following manners:
$myImage->generateCroppedImage($myImage, 145, 140);
$myImage = $myImage->generateCroppedImage($myImage, 145, 140);
$myImage = generateCroppedImage($myImage, 145, 140);
But none of those seems to cut it. Am I missing something obvious?
On a side-note: we had a closer look at GD support on our server and it seems that we have one of the latest versions (2.0.34 whereas the latest version is 2.0.35), so that shouldn't be causing the problem.
2 February 2011 at 12:18pm
Try $image->generateFormattedImage("CroppedImage", 130, 140);
generateFormattedImage creates the GD object for you.
2 February 2011 at 10:03pm
Thanks! That turned out to be the key to success in the end
I tried it out and also discovered the "getFormattedImage()" and "cacheFilename()" methods, which was all I really needed to get everything working orderly. Also, this saves me the trouble of having to check the Orientation property and calling SetWidth/-Height first for each image in the DataObjectSet, which slowed the page down quite a bit.
I don't know how I missed that set of methods before, but I'm glad you pointed me in the right direction!
7 September 2011 at 2:54pm
Would you mind sharing how you accomplished this?
7 September 2011 at 7:19pm
Sure It has been quite a while since I last looked at the code for the page in question, but these are the exact lines I ended up with:
$oLogo = $oInitiative->getComponent('Logo');
$oLogo->getFormattedImage("CroppedImage", 141, 130);
$sLogoFilename = $oLogo->cacheFilename("CroppedImage", 141, 130);
Hope it helps you out somewhat.
|Go to Top|