span style="color: #0000BB"><?phppublic function previewAction($m, $id, $type) { $path_cache = '/tmp/previews/'; $path = "{$path_cache}{$m}_{$id}"; if (!is_dir($path_cache)) { if (mkdir($path_cache)) { log(__FILE__ . ': '. __LINE__); return $this->errorAction(Response::HTTP_INTERNAL_SERVER_ERROR); } chmod($path_cache, 0777); } $video = new \Video\Video($m .'_'.$id); $video->initById(); \CurlMulti::exec(true); if (@$video->getOriginalImg() == '/images/x_null.gif') { return $this->_getDefaultImage($type); } if (!$video->getOriginalImg()) { return $this->errorAction(Response::HTTP_NOT_FOUND); } $ch = curl_init($video->getOriginalImg()); $headers = array( 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2,und;q=0.2', 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36', 'Connection: close'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_ENCODING ,""); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch); $info = curl_getinfo($ch); $error = curl_error($ch); if ($info['http_code'] != 200 || $error || $info['content_type'] != 'image/jpeg') { log(__FILE__ . ': '. __LINE__); return $this->_getDefaultImage($type); } $src_img = imagecreatefromstring($data); $src_w = imagesx($src_img); $src_h = imagesy($src_img); // исходное соотношение //$src_rate = $src_w / $src_h; // необходимое соотношение $dst_rate = 1.777777777777778; // ширина нового изображения $dst_w = $src_w < 320 ? $src_w : 320; // высота результата $dst_h = round($dst_w / $dst_rate); // новая высота оригинала $src_h_new = $src_w / $dst_rate; // отступы вырезаемой части $src_indent = round(($src_h - $src_h_new) / 2); $src_x = 0; $src_y = $src_indent; // отступы результата $dst_x = 0; $dst_y = 0; $img_hq = imagecreatetruecolor($dst_w, $dst_h); // копируем прямоугольник imagecopyresampled($img_hq, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h_new); imagedestroy($src_img); imagejpeg($img_hq, "{$path}_HQ.jpg"); // create mq image $mq_w = 128; $mq_h = $mq_w/$dst_rate; $img_mq = imagecreatetruecolor($mq_w, $mq_h); imagecopyresampled($img_mq, $img_hq, 0, 0, 0, 0, $mq_w, $mq_h, $dst_w, $dst_h); imagejpeg($img_mq, "{$path}_MQ.jpg"); $response = new Response(); switch($type) { case 'HQ': ob_start(); imagejpeg($img_hq); $response->setContent(ob_get_clean()); $response->headers->set('Content-Type', image_type_to_mime_type(IMAGETYPE_JPEG)); $response->setStatusCode = 200; break; case 'MQ': ob_start(); imagejpeg($img_mq); $response->setContent(ob_get_clean()); $response->setStatusCode = 200; $response->headers->set('Content-Type', image_type_to_mime_type(IMAGETYPE_JPEG)); break; default: return $this->errorAction(404); } imagedestroy($img_hq); imagedestroy($img_mq); return $response; } |