コード例 #1
0
ファイル: functions.php プロジェクト: saggim/TinyIB
function createThumbnail($name, $filename, $new_w, $new_h)
{
    $system = explode(".", $filename);
    $system = array_reverse($system);
    if (preg_match("/jpg|jpeg/", $system[0])) {
        $src_img = imagecreatefromjpeg($name);
    } else {
        if (preg_match("/png/", $system[0])) {
            $src_img = imagecreatefrompng($name);
        } else {
            if (preg_match("/gif/", $system[0])) {
                $src_img = imagecreatefromgif($name);
            } else {
                return false;
            }
        }
    }
    if (!$src_img) {
        fancyDie("Unable to read uploaded file during thumbnailing. A common cause for this is an incorrect extension when the file is actually of a different type.");
    }
    $old_x = imageSX($src_img);
    $old_y = imageSY($src_img);
    if ($old_x > $old_y) {
        $percent = $new_w / $old_x;
    } else {
        $percent = $new_h / $old_y;
    }
    $thumb_w = round($old_x * $percent);
    $thumb_h = round($old_y * $percent);
    $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
    fastImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
    if (preg_match("/png/", $system[0])) {
        if (!imagepng($dst_img, $filename)) {
            return false;
        }
    } else {
        if (preg_match("/jpg|jpeg/", $system[0])) {
            if (!imagejpeg($dst_img, $filename, 70)) {
                return false;
            }
        } else {
            if (preg_match("/gif/", $system[0])) {
                if (!imagegif($dst_img, $filename)) {
                    return false;
                }
            }
        }
    }
    imagedestroy($dst_img);
    imagedestroy($src_img);
    return true;
}
コード例 #2
0
ファイル: posts.php プロジェクト: nan0desu/xyntach
/**
 * Create a thumbnail
 *
 * @param string $name File to be thumbnailed
 * @param string $filename Path to place the thumbnail
 * @param integer $new_w Maximum width
 * @param integer $new_h Maximum height
 * @return boolean Success/fail
 */
function createThumbnail($name, $filename, $new_w, $new_h)
{
    if (KU_THUMBMETHOD == 'imagemagick') {
        $convert = 'convert ' . escapeshellarg($name);
        if (!KU_ANIMATEDTHUMBS) {
            $convert .= '[0] ';
        }
        $convert .= ' -resize ' . $new_w . 'x' . $new_h . ' -quality ';
        if (substr($filename, 0, -3) != 'gif') {
            $convert .= '70';
        } else {
            $convert .= '90';
        }
        $convert .= ' ' . escapeshellarg($filename);
        exec($convert);
        if (is_file($filename)) {
            return true;
        } else {
            return false;
        }
    } elseif (KU_THUMBMETHOD == 'gd') {
        $system = explode(".", $filename);
        $system = array_reverse($system);
        if (preg_match("/jpg|jpeg/", $system[0])) {
            $src_img = imagecreatefromjpeg($name);
        } else {
            if (preg_match("/png/", $system[0])) {
                $src_img = imagecreatefrompng($name);
            } else {
                if (preg_match("/gif/", $system[0])) {
                    $src_img = imagecreatefromgif($name);
                } else {
                    return false;
                }
            }
        }
        if (!$src_img) {
            exitWithErrorPage(_gettext('Unable to read uploaded file during thumbnailing.'), _gettext('A common cause for this is an incorrect extension when the file is actually of a different type.'));
        }
        $old_x = imageSX($src_img);
        $old_y = imageSY($src_img);
        if ($old_x > $old_y) {
            $percent = $new_w / $old_x;
        } else {
            $percent = $new_h / $old_y;
        }
        $thumb_w = round($old_x * $percent);
        $thumb_h = round($old_y * $percent);
        $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
        fastImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y, $system);
        if (preg_match("/png/", $system[0])) {
            if (!imagepng($dst_img, $filename, 0, PNG_ALL_FILTERS)) {
                echo 'unable to imagepng.';
                return false;
            }
        } else {
            if (preg_match("/jpg|jpeg/", $system[0])) {
                if (!imagejpeg($dst_img, $filename, 70)) {
                    echo 'unable to imagejpg.';
                    return false;
                }
            } else {
                if (preg_match("/gif/", $system[0])) {
                    if (!imagegif($dst_img, $filename)) {
                        echo 'unable to imagegif.';
                        return false;
                    }
                }
            }
        }
        imagedestroy($dst_img);
        imagedestroy($src_img);
        return true;
    }
    return false;
}
コード例 #3
0
ファイル: Photo.php プロジェクト: worksmarter/Lychee
 /**
  * @return boolean Returns true when successful.
  */
 private function createThumb($url, $filename, $type, $width, $height)
 {
     // Call plugins
     Plugins::get()->activate(__METHOD__, 0, func_get_args());
     // Quality of thumbnails
     $quality = 90;
     // Size of the thumbnail
     $newWidth = 200;
     $newHeight = 200;
     $photoName = explode('.', $filename);
     $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
     $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
     // Create thumbnails with Imagick
     if (Settings::hasImagick()) {
         // Read image
         $thumb = new Imagick();
         $thumb->readImage($url);
         $thumb->setImageCompressionQuality($quality);
         $thumb->setImageFormat('jpeg');
         // Remove metadata to save some bytes
         $thumb->stripImage();
         // Copy image for 2nd thumb version
         $thumb2x = clone $thumb;
         // Create 1st version
         $thumb->cropThumbnailImage($newWidth, $newHeight);
         $thumb->writeImage($newUrl);
         $thumb->clear();
         $thumb->destroy();
         // Create 2nd version
         $thumb2x->cropThumbnailImage($newWidth * 2, $newHeight * 2);
         $thumb2x->writeImage($newUrl2x);
         $thumb2x->clear();
         $thumb2x->destroy();
     } else {
         // Create image
         $thumb = imagecreatetruecolor($newWidth, $newHeight);
         $thumb2x = imagecreatetruecolor($newWidth * 2, $newHeight * 2);
         // Set position
         if ($width < $height) {
             $newSize = $width;
             $startWidth = 0;
             $startHeight = $height / 2 - $width / 2;
         } else {
             $newSize = $height;
             $startWidth = $width / 2 - $height / 2;
             $startHeight = 0;
         }
         // Create new image
         switch ($type) {
             case 'image/jpeg':
                 $sourceImg = imagecreatefromjpeg($url);
                 break;
             case 'image/png':
                 $sourceImg = imagecreatefrompng($url);
                 break;
             case 'image/gif':
                 $sourceImg = imagecreatefromgif($url);
                 break;
             default:
                 Log::error(Database::get(), __METHOD__, __LINE__, 'Type of photo is not supported');
                 return false;
                 break;
         }
         // Create thumb
         fastImageCopyResampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize);
         imagejpeg($thumb, $newUrl, $quality);
         imagedestroy($thumb);
         // Create retina thumb
         fastImageCopyResampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth * 2, $newHeight * 2, $newSize, $newSize);
         imagejpeg($thumb2x, $newUrl2x, $quality);
         imagedestroy($thumb2x);
         // Free memory
         imagedestroy($sourceImg);
     }
     // Call plugins
     Plugins::get()->activate(__METHOD__, 1, func_get_args());
     return true;
 }