Bringing Magick to GD

ImageMagick straightens the skew

Building a component for a client of our’s (Take on the Night) for merging some images together to get a kind of cut-out effect, at first I went straight to the known and “beloved” GD-Library. About the struggles, that should follow, and why I jumped behind enemy lines (this is where the Magick happens), I’m going to tell you in a moment’s time.

Unpacking the bundle

As the library formerly known as “GIF Draw” is bundled into PHP since version 4.3, I used it out of habit for our client’s project. As I’ve already shown in a post on my own blog a few months back, GD is quite easy to grasp and use. Weren’t it for a few flaws in the bundled library, like the following, we certainly would have sticked to it.

For neat simple copying of one image to another I have never run into problems. But for the project at hand the objects to be pasted needed a little rotation. And here the problems began.

Upping the Set

Here’s our basic code, up to the point, where we’ll begin turning the screws:

/* Keeping it simple, we bundle all our * object Info into an array */ $imageObject = array( 'width' => 245.50001525879, 'height' => 92.5, 'zoom' => 0.5, 'rotation' => -10, 'left' => 75, 'top' => 125 );

// Our background file and our object $backgroundFile = ‘background.jpg’; $objectFile = ‘object.png’;

Let the games begin

And now the fun part begins, let’s load our image and process it:

// Load the background image from a string // so we won't have to think about the format $boximage = imagecreatefromstring(file_get_contents($backgroundFile));

// The image object has larger proportions than // we wan’t it to, so we have to scale it down. $objectWidth = ceil($imageObject[‘width’] * $imageObject[‘zoom’]); $objectHeight = ceil($imageObject[‘height’] * $imageObject[‘zoom’]);

// Now create an objectImage with our // calculated dimensions and load our object. $objectImage = imagecreatetruecolor($objectWidth, $objectHeight); $tempImage = imagecreatefromstring(file_get_contents($objectFile));

// Copying and resampling our object with // preserved alpha-channel imagecolortransparent($objectImage, imagecolorallocatealpha($objectImage, 0, 0, 0, 127)); imagealphablending($objectImage, false); imagesavealpha($objectImage, true); imagecopyresampled($objectImage, $tempImage, 0, 0, 0, 0, $objectWidth, $objectHeight, imagesx($tempImage), imagesy($tempImage));

Up until now there isn’t much to worry about – preserving the PNG’s alpha-channel might be the most troublesome part, but I think the  PHP documentation, specifically the comments  there, has this covered. To see our progress till now, let’s paste the object:

The bold and the beautiful

imagealphablending($boxImage, true); imagecopy($boxImage, $objectImage, $imageObject['left'], $imageObject['top'], 0, 0, $objectWidth, $objectHeight);

… and flush our image to the browser:

// Flush the image to the browser. header('Content-type: image/jpeg'); imagejpeg($boximage); imagedestroy($boximage);

Try it on your server and you’ll get something like this “beauty”:

Flop-ROD

Now here comes the part which “breaks” our neat script – “Rotation of Death”:

$angle = $imageObject['rotation'] % 360; $objectRotated = imagerotate($objectImage, -$angle, imagecolorallocatealpha($objectImage, 0, 0, 0, 127)); imagealphablending($objectRotated, false); imagesavealpha($objectRotated, true);

// The rotated object has other dimensions // than the original image so for placing // it at the correct position, we have // ​​to calculate the difference. $diffx = (imagesx($objectRotated) – $objectWidth)/2; $diffy = (imagesy($objectRotated) – $objectHeight)/2;

// And copy it to our background. imagealphablending($boximage, true); imagecopyresampled($boximage, $objectRotated, $imageObject[‘left’]-$diffx, $imageObject[‘top’]-$diffy, 0, 0, imagesx($objectRotated), imagesy($objectRotated), imagesx($objectRotated ), imagesy($objectRotated));

When using this code instead of the simple paste operation, one should think we would get a nicely rotated object and were finished with our little project. But flush the image to the browser and you are going to get this:

As you can plainly see, GD did us no favor and cropped the rotated object. Sad, but that’s how it is. I researched some other methods and even started writing my own rotate functionality, but getting this to work with nice interpolation would have gone beyond the constraints of the client’s project.

So  ImageMagick  to the rescue!

Sadly, ImageMagick isn’t bundled with PHP, so you’ve got to install it first – but again, read the  comments to the PHP documentation , which should get you going soon.

Ready for the Magick?

We’re gonna work on the same foundation as before, so I’ll exclude the basic setup variables and jump straight into loading and resizing our background image.

imagecreatefromstring becomes:

// Load the background image into a // new ImageMagick object $boxImage = new Imagick($backgroundFile);

And now the fun part: ImageMagick terrifically releases us of having to copy our object image around just for having it resized:

// Load the object image with the new // dimensions applied $objectImage = new Imagick($objectFile); $objectImage->resizeImage($objectWidth, $objectHeight, Imagick::FILTER_LANCZOS, 1);

Easy as that. And copying an image onto another is just as straightforward:

$boxImage->compositeImage($objectImage, imagick::COMPOSITE_DEFAULT, $imageObject['left'], $imageObject['top']);

Grub first, then ethics.

Flushing the boxImage to the browser is done with a little more code than as with GD:

// Flush the image to the browser header('Content-type: image/jpeg'); $boxImage->setImageFormat('jpeg'); $boxImage->setImageCompressionQuality(100); echo $boxImage->getImageBlob();

The function  getImageBlob returns the final image as a string, just as  imagejpeg does in GD. With ImageMagick you just have to set the image format beforehand.

It’s a wee bit cleaner than with GD, in my humble opinion, but that’s not the point. Because when we now add a little rotation…

Flushing without blushing

// Rotate the Image - as simple as that $objectImage->rotateImage(new ImagickPixel('#00000000'), $imageObject['rotation']);

… where  ImagickPixel creates the alpha channel for our image object. We’ll get our final image – with no more cropped edges:

Working with images in PHP is really nice with GD – but I think ImageMagick just sugar-coats the process.

I’m a convert – will you be?

Beach meets marketing at Digimarcon West 2015

From May 27th to 28th I attended the digital marketing conference DIGIMARCON West at the Loews Hotel in Santa Monica L.A. The post comes a little late but I won’t withhold my impressions on this event organized by Aaron Polmeer.

On wednesday the attendees arrived for a cocktail come together on the pool deck. We got rewarded with a beautiful view on the Santa Monica pier, snacks and great talks. One thing I won’t forget was Jacob Curtis’ question, if we are already prepared for the heavy session set. One look at the agenda shaked me up, there were 17 sessions coming up. But instead of bothering you now with 17 summaries I picked a few interesting topics worth reading.

Content marketing – you should be the signal instead of the noise

One of these exceptions was Rand Fishkin from the inbound marketing software provider MOZ.

! In a world where content creation gets every day easier it gets also every day harder to stand out! Rand Fishkin

One way is to try reaching people before they come into a funnel. But how? Don’t focus on the next e.g. following user, find out who influences people and how. Find new platforms and try to build tactics or do things even if they don’t scale at the first moment. They will have a long term effect. Another tip is to use visual content. It can be consumed much faster and people trust images or videos more than written one. So it wonders no one that in the last years the fastest growing platforms are visually driven. Rand also gave us the tip to get in touch with new companies or other influencers before they become famous and their fans choke in the noise of other brands.

Be aware that people will trust your brand no matter what you do. When a brand has reached such a position as a trustworthy company you can find out for yourself by searching something in the web. You get several results, but which of them will you click? The one with the exact title or the one from the brand you trust the most.Notice: The less directly a process leads to scale, the less competition it has. It is hard work to bring a brand to life and it needs even more investment to keep this brand on top, ut it is worth it.

Big Data changes the world of marketing and sales

Another one or better two sessions dealt with Big Data analytics and the effect on marketing and sales in the future. Therefore I have to mention that I visited the Big Data Marketing Day in Vienna three months ago. For those interested, I already wrote a blog post, sorry it’s only in German. Big Data is around us and involves a lot answers to unsolved questions. Let me quote Sameer Khan, Senior Manager in Analytics and Marketing from IBM:

! 51 % of companies say they have a holistic view on their customers but only 37 % of customers say, that companies understand them. Sameer Khan

You see, there is a huge gap in the perceptions of both sides. Due to Big Data analytics Netflix already knew before the first day of shooting the series House of Cards that it will be a success. Big Data can provide users with the right products in a webshop, offers the right information at the right time and many more. Therefore companies need to collect data.

! Marketing should not be just a process, it has to create assets. Forest Cassidy

Inbound marketing helps to tie these assets to social profiles. Enriched with the other data (open data and others) you will get a stack that determines your marketing and business strategies and helps you make decisions even if there is no visible signal to humans. Companies that already collect data will not be ahead from now on!

Rethinking webdesign

When relaunching websites, requirements changes during the development process. And here I don’t mean a micro site, but rather corporate sites, integrated CRM solution, e-commerce systems and so on. So it is very hard and highly cost intensive to determine every single task at the beginning. And then you have a perfect plan with thousand of working hours invested but no working system. And in the end, let’s say a year or more, the system is old before it gets launched and can’t support the business processes anymore and needs expensive reeingineering. As business or marketing processes changes nearly every month, requirements for websites do too. Decision makers have to forget planned costs upfront, as these times are over. Agile workflows dominate the software industry and a start with a minimum viable product is the only approach for success.

A good strategy involving all departments and based on data is essential. Make sketches, plan tasks for first sprints and provide just the necessary design, if possible in form of living style guides to make fast and cost effective changes if necessary. For the initial launch the site needs to have just the core functionality to get your business going. In the software business the “80/20” rule or Pareto principle exists, so in the beginning don’t care for all wishes from a developer, a marketer or a sales guy, especially not from the owner. Just go live, make tests (e.g. A/B), collect data, improve, test more, evaluate the results and enhance the site in future sprints with additional functionality. And as I mention in the Big Data paragraph: a website should not be static. Offer CTAs, different landing pages, user paths and provide relevant content to each user. Cheaper but less successful will be a separation by user group or country and so on. If any feature or assumption of your prior website strategy doesn’t work – kill it. In the end this tactic will lead in a high performing website creating not only traffic but also leads and conversions. That approach is far more cost efficient than a 500 site system specification and a bunch of wonderful Design-PSDs requiring enormous budget at the end. This also the reason why Luke Summerfield, Program Manager at HubSpot recommends growth driven design. It minimizes the risk, provides the chance of continuously learning and improving the system and delivering necessary feedback for the marketing and sales department for future decisions.

Wearables and the future of media consumption

Finally I want to mention two other great sessions relating to wearables and the associated changes in our and especially in a marketers everyday life. One was from Loni Stark, Senior Director of Strategy & Product Marketing at Adobe and my absolute favorite of this conference came from Mark Schaefer, Executive Director at Schaefer Marketing Solutions.

Adobe forecasts 35 billion connected devices by 2020. That will be more than 4 devices per person – worldwide. As new applications and sensors are to be developed, adding more value to the devices, it becomes more and more interesting. At this point I have to mention the new Google Lab project Soli. We can just imagine, what devices we will have in 2020. Awesome, isn’t it?

One thing Loni mentioned made me rethinking a special topic. Where is the success of Uber coming from? It’s not to hire a taxi. It’s about having a remote control for a car. This statement has so much truth value. We will not just consume content anymore, we will control the physical world and enable other systems to provide us the information we want and need at a specific moment.

Truly the best was saved for last. So Mark Schaefer entered the stage. By the way, he is a great speaker and entertainer but he may frustrated a lot of people in the room with his comments. Some marketers just have found a working strategy and soon they will stand at the same position years ago. We already entered the change, at it feels like the phase when Facebook & co. entered the market. Now the same happens with wearables and the way we consume media. On average we consume 14,6 hours of content a day. That’s a lot and there is not much space upwards. And the cut of delivering content already happens like on Facebook. They claim that a single user only can scan max. 1500 posts a day. So how will it work to be within the x percent people get delivered? Sales messages aren’t worth spreading, but creativity and useful information do so. People don’t share because they want to help you selling your goods. They do it because of intrinsic emotional reasons. We have to break new grounds, should have a look at new technologies, provide different content types and the real essential things, find a unsaturated niche and build trust with the creative content we deliver. In the future no one will visit special networks, I guess all networks will work together with the devices and provide us information on our health, the posts of our friends or essential information for every-day live without an opener like in the past. So that’s also the reason why QR or barcode scanning didn’t work for providing additional information in marketing. And please guys out there, stop using QR codes on our websites, newsletters or other media. Why?

Summarized, the conference was not as informational as expected. I was excited to hear a lot of technology news, insights in new trends and some tips to develop my skills, but instead many sessions were just sales presentations on products and approaches that were already outdated. Also content marketing is not the new hot trend, but already the big hype as social media was some years ago. Most disturbing of all was the session of Kevin Jonas and Adam Gausepohl on Snapchat and it’s success within younger age groups. Please don’t get me wrong. This app and it’s concept of disappearing content is great but the performance of the speakers was horrible. You really felt the bundle of dollars in their back pockets they got to promote the app.

Aaron Polmeer announced a possible conference in New York and I hope for him and all possible attendants that there will be more speakers like Rand, Mark, Loni and Simon. But on the other hand, if there would have been no conference I maybe not have visited Santa Monica this soon. 🙂