Alish
Alish

Reputation: 240

How to I Optimize Response of PHP Code

I have a Code to Rebuild Image in PHP but they work Slowly
This is Demo URL of Image

and Original URL is Work Very Fastly Original URL of Image

I'm try this Code for Rebuild Image with my Own Custom URL

<?php

if(!isset($_GET['v']))      
exit('VIDEO ID IS NOT EXIST');  
$v = $_GET['v'];
$fmt = $_GET['fmt'];
$url = 'http://ytimg.googleusercontent.com/vi/'.$v.'/'.$fmt;
$data = file_get_contents ($url);
Header ("Content-type: image/jpeg");
echo $data;

?>

but this code is work very slowly Image loading is Slow how can i optimized image response fastly

Upvotes: 1

Views: 106

Answers (2)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23389

This might be a little faster than using file_get_contents.

<?php
if(!isset($_GET['v'])) exit('VIDEO ID IS NOT EXIST');  
Header ("Content-type: image/jpeg");
readfile("http://ytimg.googleusercontent.com/vi/{$_GET['v']}/{$_GET['fmt']}");

Upvotes: 1

Awais Mustafa
Awais Mustafa

Reputation: 151

The remote webs server closes the connection aftr 15 seconds and until then, it keeps alive. So the solution for this is to tell server to close the connection after every request. This may solve your issue.

$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
file_get_contents($url,false,$context);

Upvotes: 0

Related Questions