Skip to content

Instantly share code, notes, and snippets.

@renekreijveld

renekreijveld/createthumbs

Last active Aug 29, 2015
Embed
What would you like to do?
Create smaller image and thumbnail from file upload field in RSForm!Pro
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType){
if($CurWidth <= 0 || $CurHeight <= 0){
return false;
}
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight)){
switch(strtolower($ImageType)){
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType){
if($CurWidth <= 0 || $CurHeight <= 0){
return false;
}
if($CurWidth>$CurHeight){
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size= $CurWidth - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size)){
switch(strtolower($ImageType)){
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
function sanitize_file_name($string) {
$clean = preg_replace("/[^a-zA-Z0-9\._-]/", "_", $string);
return strtolower($clean);
}
function createCode($length) {
$chars = "1234567890abcdefghijkmnopqrstuvwxyz";
$i = 0;
$code = "";
while ($i <= $length) {$code .= $chars{mt_rand(0,strlen($chars))};$i++;}
return $code;
}
if($_FILES['form']['name']['photo'] != '') {
$ThumbImageSize = 80;
$BigImageMaxSize = 400;
$ThumbPrefix = "thumb-";
$DestinationDirectory = JPATH_BASE.'/images/yourphotos/';
$Quality = 90;
$code = createCode(10);
$ImageName = sanitize_file_name($_FILES['form']['name']['photo']);
$ImageSize = $_FILES['form']['size']['photo'];
$TempSrc = $_FILES['form']['tmp_name']['photo'];
$ImageType = $_FILES['form']['type']['photo'];
switch(strtolower($ImageType))
{
case 'image/png':
$CreatedImage = imagecreatefrompng($_FILES['form']['tmp_name']['photo']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES['form']['tmp_name']['photo']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['form']['tmp_name']['photo']);
break;
default:
die('Filetype not allowed!');
}
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$code.'.'.$ImageExt;
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName;
$DestRandImageName = $DestinationDirectory.$NewImageName;
if(resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType)){
if(!resizeImage($CurWidth,$CurHeight,$ThumbImageSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType)){
echo 'Error Creating thumbnail';
}
}else{
die('Resize Error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment