/**
 * Generates images set for the ad.
 * 300 x 50 Mobile leaderboard
 468 x 60 Banner
 728 x 90 Leaderboard
 250 x 250 Square
 200 x 200 Small square
 336 x 280 Large rectangle
 300 x 250 Inline rectangle
 120 x 600 Skyscraper
 160 x 600 Wide skyscraper
 *
 * @param string $source Source image file name
 */
 public function buildAds($source = '')
 {
     if (!$this->giag_id) {
         echo "No Ad Group ID";
         return false;
     }
     static $patch;
     if (!$patch) {
         $patch = imagecreatefrompng("../output/patterns/lmhira.png");
     }
     $sizes = array(0 => array("width" => 200, "height" => 200), 1 => array("width" => 250, "height" => 250), 2 => array("width" => 336, "height" => 280), 3 => array("width" => 300, "height" => 250));
     for ($i = 0; $i < 5 && !$img; $i++) {
         echo "Reading image\r\n";
         $img_name = "../output/cache/" . $this->giag_id . ".jpg";
         if (!file_exists($img_name)) {
             file_put_contents($img_name, file_get_contents($source));
         }
         $img = @imagecreatefromjpeg($img_name);
     }
     if (false == $img) {
         return false;
     }
     $height = imagesy($img);
     $width = imagesx($img);
     imagecopy($img, $patch, 0, $height - 30, 0, 0, 100, 30);
     $result = true;
     foreach ($sizes as $size) {
         $ad = new GoogleImageAd($this->db);
         $ad->gia_campaign_id = $this->giag_campaign_id;
         $ad->gia_ad_group_id = $this->giag_id;
         $ad->gia_destination_url = $this->giag_destination_url;
         $ad->gia_display_url = $this->giag_display_url;
         $ad->gia_size = $size['width'] . "x" . $size['height'];
         $result &= $ad->save();
         $factor = min($height / $size['height'], $width / $size['width']);
         $wlimit = $size['width'] * $factor;
         $hlimit = $size['height'] * $factor;
         $new_img = imagecreatetruecolor($size['width'], $size['height']);
         imagecopyresized($new_img, $img, 0, 0, 0, 0, $size['width'], $size['height'], $wlimit, $hlimit);
         imagejpeg($new_img, "../output/images/" . $ad->gia_id . "_{$size['width']}x{$size['height']}.jpg");
         $ad->gia_name = "Image Ad " . $ad->gia_id . " {$size['width']}x{$size['height']}";
         $ad->gia_image = "images/" . $ad->gia_id . "_{$size['width']}x{$size['height']}.jpg";
         $result &= $ad->save();
         $this->ads[$ad->gia_id] = $ad;
         unset($ad);
         echo "Created Ad\r\n";
     }
     return $result;
 }
 function stattestGenerateImages()
 {
     $this->assertFalse(GoogleImageAd::generateImages("test_source_image2.jpg"));
     $this->assertTrue(GoogleImageAd::generateImages("test_source_image.jpg"));
     //$this->assertTrue(file_exists("../output/images/300x50.jpg"));
     $this->assertTrue(file_exists("../output/images/250x250.jpg"));
     $this->assertTrue(file_exists("../output/images/200x200.jpg"));
     $this->assertTrue(file_exists("../output/images/336x280.jpg"));
     $this->assertTrue(file_exists("../output/images/300x250.jpg"));
 }