Skip to content

Instantly share code, notes, and snippets.

@voku
Last active August 29, 2015 14:02
Show Gist options
  • Save voku/9d2fb6094092dd1f6e44 to your computer and use it in GitHub Desktop.
Save voku/9d2fb6094092dd1f6e44 to your computer and use it in GitHub Desktop.
image-resize via ImageWorkshop (PHP) - Link: http://phpimageworkshop.com/
<?php
/**
* Get the name without extension
*
* @param String $filename
*
* @return String | false (by error)
*/
function getFileName($filename)
{
$return = false;
if (($filename = basename($filename)) && ($dot_pos = strrpos($filename, "."))) {
$return = substr($filename, 0, $dot_pos);
}
return $return;
}
/**
* get file-extension
*
* @param String $filename
*
* @return String | false (by error)
*/
function getFileExtension($filename)
{
$ext = false;
if (function_exists('pathinfo') && (file_exists($filename))) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
} else {
if (stristr($filename, '.') !== false) {
$a = explode('.', $filename);
$ext = array_pop($a);
}
}
if ($ext !== false) {
$ext = strtolower($ext);
}
return $ext;
}
/**
* resize a image and save it to a new file
*
* @param String $sourceImage
* @param String $thumbNameExtra
* @param mixed $widthInPX
* @param mixed $heightInPX
* @param Int $compression
*
* @return String or false on error
*/
function imageResize($sourceImage, $thumbNameExtra = '_thumb', $widthInPX = null, $heightInPX = null, $compression = 90)
{
if ($widthInPX == null && $heightInPX == null) {
return false;
}
if (!is_file($sourceImage)) {
return false;
}
$fileName = getFileName($sourceImage);
$fileExt = getFileExtension($sourceImage);
if (!$fileName || !$fileExt) {
return false;
}
$newFileName = $fileName . $thumbNameExtra . '.' . $fileExt;
$layer = ImageWorkshop::initFromPath($sourceImage);
$layer->resizeInPixel($widthInPX, $heightInPX, true, 0, 0, 'MM');
$layer->save('/var/www/.../todo/', $newFileName, false, null, $compression);
return $newFileName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment