Last active
December 10, 2015 22:29
-
-
Save anjerodesu/4502586 to your computer and use it in GitHub Desktop.
A simple PHP class that with 2 main functions: copy an image from a website to the server and resize and crop the image to the specified size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/********************************************************************************************/ | |
/* ImageHelper created by Angelo Villegas [http://www.angelovillegas.com] */ | |
/* */ | |
/* Copyright © Angelo Villegas */ | |
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this */ | |
/* software and associated documentation files (the "Software"), to deal in the Software */ | |
/* without restriction, including without limitation the rights to use, copy, modify, */ | |
/* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to */ | |
/* permit persons to whom the Software is furnished to do so, subject to the following */ | |
/* conditions: */ | |
/* */ | |
/* The above copyright notice and this permission notice shall be included in all copies */ | |
/* or substantial portions of the Software. */ | |
/* */ | |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, */ | |
/* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A */ | |
/* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */ | |
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ | |
/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE */ | |
/* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | |
/********************************************************************************************/ | |
class ImageHelper | |
{ | |
function saveImage( $url = null , $imageURL = null ) | |
{ | |
// any can not be null | |
if ( is_null( $url ) || is_null( $imageURL ) ) | |
{ | |
return false; | |
} | |
else | |
{ | |
$curl = curl_init( $url ); // initialise curl using the URL | |
$file = fopen( $imageURL , 'wb' ); // open the image path, create if it does not exist | |
curl_setopt( $curl , CURLOPT_FILE , $file ); // setup the file to be written to | |
curl_setopt( $curl , CURLOPT_HEADER , false ); // include header, false if not | |
curl_setopt( $curl , CURLOPT_FOLLOWLOCATION , true ); // true to follow any "location" the server sent | |
curl_exec( $curl ); | |
curl_close( $curl ); | |
fclose( $file ); | |
return true; | |
} | |
} | |
function resizeCrop( $image , $savePath = null , $crop = null , $size = null ) | |
{ | |
$image = imagecreatefromstring( file_get_contents( $image ) ); | |
if ( is_resource( $image ) === true ) // check if the image is a resource | |
{ | |
$x = 0; | |
$y = 0; | |
$width = imagesx( $image ); | |
$height = imagesy( $image ); | |
// crop | |
if ( is_null( $crop ) === true ) // check if $crop is null | |
{ | |
$crop = array( $width , $height ); | |
} | |
else | |
{ | |
$crop = array_filter( explode( ':' , $crop ) ); // explode crop ratio | |
if ( empty( $crop ) === true ) | |
{ | |
$crop = array( $width , $height ); | |
} | |
else | |
{ | |
// check wether empty or non-numeric | |
if ( ( empty( $crop[0] ) === true ) || ( is_numeric( $crop[0] ) === false ) ) | |
{ | |
$crop[0] = $crop[1]; | |
} | |
else if ( ( empty( $crop[1] ) === true ) || ( is_numeric( $crop[1] ) === false ) ) | |
{ | |
$crop[1] = $crop[0]; | |
} | |
} | |
$ratio = array( 0 => $width / $height , 1 => $crop[0] / $crop[1] ); // initialise ratio | |
if ( $ratio[0] > $ratio[1] ) | |
{ | |
$width = $height * $ratio[1]; | |
$x = ( imagesx ( $image ) - $width ) / 2; | |
} | |
else if ($ratio[0] < $ratio[1]) | |
{ | |
$height = $width / $ratio[1]; | |
$y = ( imagesy( $image ) - $height ) / 2; | |
} | |
} | |
// resize | |
if ( is_null( $size ) === true ) // check if $size is null | |
{ | |
$size = array( $width , $height ); | |
} | |
else | |
{ | |
$size = array_filter( explode( 'x' , $size ) ); // explode size in pixels | |
if ( empty( $size ) === true ) | |
{ | |
$size = array( imagesx( $image ) , imagesy( $image ) ); | |
} | |
else | |
{ | |
if ( ( empty( $size[0] ) === true ) || ( is_numeric( $size[0] ) === false ) ) | |
{ | |
$size[0] = round( $size[1] * $width / $height ); | |
} | |
else if ( ( empty( $size[1] ) === true ) || ( is_numeric( $size[1] ) === false ) ) | |
{ | |
$size[1] = round( $size[0] * $height / $width ); | |
} | |
} | |
} | |
// create image | |
$result = imagecreatetruecolor( $size[0] , $size[1] ); | |
if ( is_resource( $result ) === true ) | |
{ | |
imagesavealpha( $result , true ); | |
imagealphablending( $result , true ); | |
imagefill( $result , 0 , 0 , imagecolorallocate( $result , 255 , 255 , 255 ) ); | |
imagecopyresampled( $result , $image , 0 , 0 , $x , $y , $size[0] , $size[1] , $width , $height ); | |
imageinterlace( $result , true ); | |
imagejpeg( $result , $savePath , 90 ); | |
imagedestroy( $result ); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment