Skip to content

Instantly share code, notes, and snippets.

@jleonardolemos
Created December 11, 2016 15:01
Show Gist options
  • Save jleonardolemos/97ec51464ec7f81b5d6999b11b778f2f to your computer and use it in GitHub Desktop.
Save jleonardolemos/97ec51464ec7f81b5d6999b11b778f2f to your computer and use it in GitHub Desktop.
//returns cropped image
function getPropertyImage($property)
{
return Image::url(getImagePath($property), 315, 235, ['crop']);
}
//retorna o path da imagem, faz download da imagem
function getImagePath($property)
{
//if the property dont has an image, returns no-image.jpj
if (!isset($property->galleries[0]->images[0]->name)) {
return '/assets/img/sem-imagem.jpg';
}
$path = public_path(
'properties/'
. $property->advertiser_id . '/'
. $property->id . '/'
. basename($property->galleries[0]->images[0]->name)
);
//if the file dont exists in local path, copy from remoto to local path
if (!file_exists($path)) {
mkdir(dirname($path), 0775, true);
file_put_contents($path, file_get_contents(env('API_URL') . $property->galleries[0]->images[0]->name));
}
//returns the relative path
return str_replace(public_path(), '', $path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment