views:

185

answers:

1

Hey

I have some code that downloads an image from a remote server

$data = file_get_contents("$rURL");

I want to then change the quality of this image, but do not want to save it out to a file first, how do I convert $data into an image that I can then use in imagecopyresampled?

Thanks

+4  A: 

Try

imagecreatefromstring — Create a new image from the image stream in the string

Example from PHP manual:

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);

As an alternative use any of the imagecreatefrom* functions if you know the image format in advance to load the URL directly, e.g.

imagecreatefrompng('http://example.com/image.png')
Gordon