Ejemplo n.º 1
0
 /**
  *  Resize an image to the specified dimensions, placing the resulting
  *  image in the specified location.  At least one of $newWidth or
  *  $newHeight must be specified.
  *
  *  @param  string  $type       Either 'thumb' or 'disp'
  *  @param  integer $newWidth   New width, in pixels
  *  @param  integer $newHeight  New height, in pixels
  *  @return string  Blank if successful, error message otherwise.
  */
 public static function ReSize($src, $dst, $newWidth = 0, $newHeight = 0)
 {
     global $_LGLIB_CONF;
     // Calculate the new dimensions
     $A = self::reDim($src, $newWidth, $newHeight);
     if ($A === false) {
         COM_errorLog("Invalid image {$src}");
         return 'invalid image conversion';
     }
     list($sWidth, $sHeight, $dWidth, $dHeight) = $A;
     // Get the mime type for the glFusion resizing functions
     $mime_type = image_type_to_mime_type(exif_imagetype($src));
     // Returns an array, with [0] either true/false and [1]
     // containing a message.
     $result = array();
     if (function_exists(_img_resizeImage)) {
         $result = _img_resizeImage($src, $dst, $sHeight, $sWidth, $dHeight, $dWidth, $mime_type);
     } else {
         $result[0] = false;
     }
     if ($result[0] == true) {
         return '';
     } else {
         COM_errorLog("Failed to convert {$src} ({$sHeight} x {$sWidth}) to {$dst} ({$dHeight} x {$dWidth})");
         return 'invalid image conversion';
     }
 }
Ejemplo n.º 2
0
/**
 * Takes an image file and resizes it
 *
 * @param   string  $srcImage       Absolute path to the source image
 * @param   string  $destImage      Absolute path to the destination image
 * @param   string  $dImageHeight   Destination image height
 * @param   string  $dImageWidth    Destination image width
 * @param   string  $mimeType       Source image mime type, if known
 * @param   bool    $deleteSrc      Should the source image be deleted after resizing
 *
 * @return  array   Returns $rc and $msg. $msg will be set if there was an error
 *
 * Note: $srcImage and $destImage can be the same location in which case the
 *       original image will be resized
 */
function IMG_resizeImage($srcImage, $destImage, $dImageHeight, $dImageWidth, $mimeType = '', $deleteSrc = 0)
{
    global $_CONF;
    $JpegQuality = 100;
    if ($dImageHeight == 0) {
        $dImageHeight = 200;
    }
    if ($dImageWidth == 0) {
        $dImageWidth = 200;
    }
    $imgsize = @getimagesize("{$srcImage}");
    $imgwidth = $imgsize[0];
    $imgheight = $imgsize[1];
    if ($imgwidth == 0 || $imgheight == 0) {
        $imgwidth = $dImageWidth;
        $imgheight = $dImageHeight;
    }
    if ($mimeType == '') {
        $metaData = IMG_getMediaMetaData($srcImage);
        $mimeType = $metaData['mime_type'];
    }
    if ($mimeType == 'image/x-targa' || $mimeType == 'image/tga') {
        $fp = @fopen($srcImage, 'rb');
        if ($fp == false) {
            return array(false, 'Failed to open source TGA image.');
        }
        $data = fread($fp, filesize($srcImage));
        fclose($fp);
        $imgwidth = base_convert(bin2hex(strrev(substr($data, 12, 2))), 16, 10);
        $imgheight = base_convert(bin2hex(strrev(substr($data, 12, 2))), 16, 10);
        COM_errorLog("TGA resolution: height: " . $imgheight . " width: " . $imgwidth);
    }
    if ($imgwidth > $imgheight) {
        $ratio = $imgwidth / $dImageWidth;
        $newwidth = $dImageWidth;
        $newheight = round($imgheight / $ratio);
    } else {
        $ratio = $imgheight / $dImageHeight;
        $newheight = $dImageHeight;
        $newwidth = round($imgwidth / $ratio);
    }
    // check to see if srcImage is smaller than desired target,
    // if smaller, do not upsize, simply copy the src to the dest.
    if ($newheight > $imgheight && $newwidth > $imgwidth) {
        if ($srcImage != $destImage) {
            $rc = copy($srcImage, $destImage);
            COM_errorLog("IMG_resizeImage: Original (" . $srcImage . ") is smaller than target, original copied to target image (" . $destImage . ".");
        }
        return array(true, 'Original is smaller than target, original copied to target image.');
    }
    list($rc, $msg) = _img_resizeImage($srcImage, $destImage, $imgheight, $imgwidth, $newheight, $newwidth, $mimeType);
    if ($rc == false) {
        return array($rc, $msg);
    }
    return array(true, 'Image successfully resized');
}