Forum Moderators: coopster
It is designed for use on a shared server, where using CHMOD() is usually disabled. If it isnt disabled, the setPermissions() function could be chopped down accordingly.
If you modify it or see anywhere it can be improved please let me know!
Enjoy!
PS sorry its so long...
<?php
class Thumb {
//the max height & width you want the thumbnail
const MAX_WIDTH = 100;
const MAX_HEIGHT = 100;
//root path. on a shared server, it would be like:
const ROOT_PATH = '/home/bla_bla/public_html';
//ftp account details
//require to CHMOD on a shared server
//where CHMOD is usually disabled
const FTP_USER_NAME = 'username';
const FTP_USER_PASS = 'password';
const FTP_ROOT = '/public_html/';
const FTP_SERVER = 'www.example.com';
function createThumb($path, $nw = '', $nh = '', $overwrite = false) {
$success = false;
$filename;
$newFilename;
$type;
$location;
$fn;
$size;
$w;
$h;
$newW;
$newH;
$xRatio;
$yRatio;
$maxH = self::MAX_HEIGHT;
$maxW = self::MAX_WIDTH;
$src;
$dst;
$exists;
if($nw!= '')
$maxW = $nw;
if($nh!= '')
$maxH = $nh;
//validate $path
if(file_exists($path)) {
//get required components of filename
$filename = $this->getFileName($path);
$newFilename = self::ROOT_PATH.$filename[0].$filename[2];
$type = $this->getFileExt($filename[1]);
//get image size
$size = GetImageSize($path);
$w = $size[0];
$h = $size[1];
//calc new image size
$x Ratio = $maxW / $w;
$yRatio = $maxH / $h;
if($w <= $maxW && $h <= $maxH) {
$newW = $w;
$newH = $h;
} else {
$newW = $maxW;
$newH = ceil($xRatio * $h);
}
//check to see if $newFilename exists
$exists = file_exists($newFilename);
//create the thumb only if it doesnt already exist (or overwrite is on)
if(!$exists ¦¦ ($exists && $overwrite)) {
//create image relevant to filetype
switch($type) {
case 'jpg':
case 'pjpg':
case 'jpeg':
case 'pjpeg':
//open the source image
$src = ImageCreateFromJpeg($path);
//create destination canvass
$dst = ImageCreateTrueColor($newW, $newH);
//copy and resize source to destination
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);
//set permissions and write the image to disk
$this->setPermission($filename[0], '0757');
if(ImageJpeg($dst, $newFilename))
$success = true;
//reset permissions
$this->setPermission($filename[0], '0755');
break;
case 'png':
//see comments above for explanation
$src = ImageCreateFromPng($path);
$dst = ImageCreate($newW, $newH);
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);
$this->setPermission($filename[0], '0757');
if(ImagePng($dst, $newFilename))
$success = true;
$this->setPermission($filename[0], '0755');
break;
case 'gif':
$src = ImageCreateFromGif($path);
$dst = ImageCreate($newW, $newH);
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);
$this->setPermission($filename[0], '0757');
if(ImageGif($dst, $newFilename))
$success = true;
$this->setPermission($filename[0], '0755');
break;
default:
print('Invalid file extension');
}
//clean up resources
ImageDestroy($src);
ImageDestroy($dst);
} else {
print('File already exists');
}
}
return $success;
}
function setPermission($path, $mod) {
$ftp_user_name = self::FTP_USER_NAME;
$ftp_user_pass = self::FTP_USER_PASS;
$ftp_root = self::FTP_ROOT;
$ftp_server = self::FTP_SERVER;
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to chmod $path directory
if (ftp_site($conn_id, 'CHMOD '.$mod.' '.$ftp_root.$path)!== false) {
$success = true;
}
else {
$success= false;
}
// close the connection
ftp_close($conn_id);
return $success;
}
function getFileExt($file) {
//return file extension from given filename
$ext = strtolower(substr($file, (strpos($file, '.') + 1)));
return $ext;
}
function getFileName($path) {
//get required components for file manipulation
$parts = explode('/', $path);
//filename[0] is the complete path
for($i = 0; $i < count($parts) - 1; $i++) {
$filename[0] .= $parts[$i].'/';
}
//...apart from ROOT_PATH
if(strpos($filename[0], self::ROOT_PATH)!== false) {
$filename[0] = substr($filename[0], strlen(self::ROOT_PATH));
}
//filename[1] = original file name
$filename[1] = $parts[(count($parts) - 1)];
$fnParts = explode('.', $parts[(count($parts) - 1)]);
//new thumbnail filename takes the following format:
// image.jpg -> image_th.jpg
//filename[2] = new filename
$filename[2] = $fnParts[0].'_th.'.$fnParts[1];
return $filename;
}
}
?>
I'm a little frustrated with the documentation for imagick...maybe someone here can help.
imagick's image resize function seems to take arbitrary (image) filetypes....is this correct? Anybody know?
It would seem that Imagick functions would save a lot of work you'd have to do using gd functions dealing with each filetype individually.
Does that make sense? seems that with gd, you have to resize jpeg, then write a whole new section to resize png, then another for gif. Imagick seems to deal with all of them in the same function, unless I'm missing something (like documentation)
actually, if anyone has any insight on using imagick, I'd love to hear it!