<?
$img = $_GET['img'];
$w = $_GET['w'];
$h = $_GET['h'];
$imgName = substr ($img, 0, -4);
$imgExtension = 'jpg';
header ("Content-type: image/jpeg");
$fileName = "_media/cache/" . $imgName . "_" . $w . "_" . $h . "." . $imgExtension;
if (file_exists ($fileName)) {
readfile ($fileName);
} else {
$im = imagecreatetruecolor ($w, $h);
$background_color = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, $w, $h, $background_color);
$oldW = 389;
$oldH = 811;
$oldAspect = $oldW / $oldH;
$newAspect = $w / $h;
if ($oldAspect < $newAspect) {
$newH = $h;
$newW = $oldW / ($oldH / $h);
$newX = ($w / 2) - ($newW / 2);
$newY = 0;
} else {
$newW = $w;
$newH = $oldH / ($oldW / $w);
$newX = 0;
$newY = ($h / 2) - ($newH / 2);
}
$im_screen = imagecreatefromjpeg ("_media/source/$img");
$im_promo = imagecreatetruecolor ($oldW, $oldH);
imagefilledrectangle($im, 0, 0, $oldW, $oldH, $background_color);
imagecopy ($im_promo, $im_screen, 36, 120, 0, 0, 320, 480);
imagecopyresampled ($im, $im_promo, $newX, $newY, 0, 0, $newW, $newH, $oldW, $oldH);
imagedestroy ($im_promo);
$im_iphone = imagecreatefrompng ("_media/iphone/iphone_overlay.png");
imagecopyresampled ($im, $im_iphone, $newX, $newY, 0, 0, $newW, $newH, $oldW, $oldH);
imagedestroy ($im_iphone);
imagejpeg ($im, $fileName);
imagedestroy($im);
readfile ($fileName);
}
?>