Skip to content

Instantly share code, notes, and snippets.

@blrobin2
Created July 4, 2015 02:15
Show Gist options
  • Select an option

  • Save blrobin2/bce5680522c08f20d373 to your computer and use it in GitHub Desktop.

Select an option

Save blrobin2/bce5680522c08f20d373 to your computer and use it in GitHub Desktop.
This is my helpers file I've been using in a few Laravel projects. Just throwing it up here because, why not?
<?php
/**
* Checks for an image and moves it to the set folder.
*
* @param $image . The image, most likely from the Request object
* @param $destination . The folder in source where it's going.
* @return null|string
*/
function moveImage($image, $destination)
{
// If image is not a resource, it's probably the string for where the image is located.
// In that case, just set it to the image location.
if (! is_resource($image)) {
return $image;
}
// Otherwise, make sure that it's set and that it's a valid image.
if ($image && $image->isValid()) {
$name = str_replace(' ', '_', $image->getClientOriginalName());
$destinationPath = 'img/' . strtolower($destination);
$image->move($destinationPath, $name);
return $destinationPath . '/' . $name;
}
// If it's not set, we're not updating it so just return nothing.
return null;
}
/**
* Converts a string with spaces to a lower-case string,
* joined by dashes.
*
* @param string $string . The string to be converted.
* @return string
*/
function nameAsLink($string)
{
return str_replace(' ', '-', strtolower($string));
}
/**
* Convert a boolean to a 'Yes' or 'No'
*
* @param bool $bool
* @return string
*/
function boolToString($bool)
{
if ($bool) {
return '<span class="text-success">Yes</span>';
}
return '<span class="text-danger">No</span>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment