Created
August 30, 2012 11:59
-
-
Save maxrice/3527161 to your computer and use it in GitHub Desktop.
Resize images to a specific size while maintaining original aspect-ratio via PHP CLI
This file contains hidden or 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 | |
// only process image files | |
$accepted_extensions = array( 'jpg', 'jpeg', 'png' ); | |
// set final size of cropped image | |
$image_size_width = 892; | |
$image_size_height = 892; | |
//only process files in directory that script is in | |
$dir = realpath( dirname( __FILE__ ) ); | |
$files = scandir( $dir ); | |
foreach( $files as $file ) { | |
// get file / path info | |
$file_info = pathinfo( $file ); | |
// only process actual files | |
if( isset( $file_info['extension'] ) AND isset( $file_info['filename'] ) ) { | |
// get the file extension | |
$extension = $file_info['extension']; | |
// only process image files with acceptable extensions | |
if( in_array( $extension, $accepted_extensions ) ) { | |
// instantiate resizer | |
$im = new CropImage( $file ); | |
//create resized jpg @ default quality and overwrite original file | |
imagejpeg( $im->crop(892,892), $file_info['filename'] . '.jpg' ); | |
// cleanup | |
unset( $im ); | |
} | |
} | |
} | |
/* | |
This class Copyright (C) 2008 Alex Poole | |
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses>. | |
*/ | |
class CropImage{ | |
//get original image & turn into a resource | |
//get required dimensions | |
//resize image to required dimensions within aspect ratio | |
//check whether width or height are wrong | |
//get number of pixels required to pad either width or height | |
//halve them (or nearly halve them, with a one pixel offset if the total padding amount is an odd number) | |
//add equal amount of whitespace on each half of the image, either vertically or horizontally | |
//return the resource | |
function __construct($imgurl){ | |
$this->imgurl = $imgurl; | |
$this->imgsize = @getImageSize($imgurl); | |
if (!$this->imgsize){ | |
return false; | |
} | |
if ( substr(strtolower($imgurl), -4) == "jpeg" || substr(strtolower($imgurl), -3) == "jpg"){ | |
$this->orig_im = ImageCreateFromJPEG($imgurl); | |
} else { | |
$this->orig_im = ImageCreateFromPNG($imgurl); | |
} | |
} | |
function crop($w, $h){ | |
$newim = $this->resizeImage($this->orig_im, $this->imgsize[0], $this->imgsize[1], $w, $h); | |
//return Array($newImage, $neww, $newh); | |
if ($newim[1] < $w || $newim[2] < $h){ | |
//add more width or height | |
//create new white image | |
$paddedimg = imagecreatetruecolor($w, $h); | |
$white = imagecolorallocate($paddedimg,255,255,255); | |
imagefill($paddedimg, 0, 0, $white); | |
//stick the thumb in the middle of the new white image | |
$pastex = floor(($w - $newim[1]) / 2); | |
$pastey = floor(($h - $newim[2]) / 2); | |
imagecopy($paddedimg, $newim[0], $pastex, $pastey, 0, 0, $newim[1], $newim[2]); | |
//imagefill($paddedimg, 0, 0, $white); | |
return $paddedimg; | |
/* | |
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct ) | |
Copy a part of src_im onto dst_im starting at the x,y coordinates src_x , src_y with a width of src_w and a height of src_h . The portion defined will be copied onto the x,y coordinates, dst_x and dst_y . | |
*/ | |
} else { | |
//pass through | |
return $newim[0]; | |
} | |
} | |
function resizeImage($image, $iw, $ih, $maxw, $maxh){ | |
if ($iw > $maxw || $ih > $maxh){ | |
if ($iw>$maxw && $ih<=$maxh){//too wide, height is OK | |
$proportion=($maxw*100)/$iw; | |
$neww=$maxw; | |
$newh=ceil(($ih*$proportion)/100); | |
} | |
else if ($iw<=$maxw && $ih>$maxh){//too high, width is OK | |
$proportion=($maxh*100)/$ih; | |
$newh=$maxh; | |
$neww=ceil(($iw*$proportion)/100); | |
} | |
else {//too high and too wide | |
if ($iw/$maxw > $ih/$maxh){//width is the bigger problem | |
$proportion=($maxw*100)/$iw; | |
$neww=$maxw; | |
$newh=ceil(($ih*$proportion)/100); | |
} | |
else {//height is the bigger problem | |
$proportion=($maxh*100)/$ih; | |
$newh=$maxh; | |
$neww=ceil(($iw*$proportion)/100); | |
} | |
} | |
} | |
else {//copy image even if not resizing | |
$neww=$iw; | |
$newh=$ih; | |
} | |
if (function_exists("imagecreatetruecolor")){//GD 2.0=good! | |
$newImage=imagecreatetruecolor($neww, $newh); | |
imagecopyresampled($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih); | |
} else {//GD 1.8=only 256 colours | |
$newImage=imagecreate($neww, $newh); | |
imagecopyresized($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih); | |
} | |
return Array($newImage, $neww, $newh); | |
} | |
}//class | |
//end file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment