Exemple #1
0
function thumbnail($image, $width, $height, $extension)
{
    $data = getImageSize($image);
    $imageInfo["width"] = $data[0];
    $imageInfo["height"] = $data[1];
    $imageInfo["type"] = $data[2];
    switch ($imageInfo["type"]) {
        case 1:
            $img = imagecreatefromgif($image);
            break;
        case 2:
            $img = imageCreatefromjpeg($image);
            break;
        case 3:
            $img = imageCreatefrompng($image);
            break;
        default:
            return false;
    }
    $size["width"] = $imageInfo["width"];
    $size["height"] = $imageInfo["height"];
    if ($width < $imageInfo["width"]) {
        $size["width"] = $width;
    }
    if ($height < $imageInfo["height"]) {
        $size["height"] = $height;
    }
    if ($imageInfo["width"] * $size["width"] > $imageInfo["height"] * $size["height"]) {
        $size["height"] = round($imageInfo["height"] * $size["width"] / $imageInfo["width"]);
    } else {
        $size["width"] = round($imageInfo["width"] * $size["height"] / $imageInfo["height"]);
    }
    $newImg = imagecreatetruecolor($size["width"], $size["height"]);
    $otsc = imagecolortransparent($img);
    if ($otsc >= 0 && $otsc <= imagecolorstotal($img)) {
        $tran = imagecolorsforindex($img, $otsc);
        $newt = imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
        imagefill($newImg, 0, 0, $newt);
        imagecolortransparent($newImg, $newt);
    }
    imagecopyresized($newImg, $img, 0, 0, 0, 0, $size["width"], $size["height"], $imageInfo["width"], $imageInfo["height"]);
    imagedestroy($img);
    $newName = str_replace('.', $extension . '.', $image);
    switch ($imageInfo["type"]) {
        case 1:
            $result = imageGif($newImg, $newName);
            break;
        case 2:
            $result = imageJPEG($newImg, $newName);
            break;
        case 3:
            $result = imagepng($newImg, $newName);
            break;
    }
    imagedestroy($newImg);
}
 if (file_exists($thumbnail)) {
     // echo "The thumbnail $thumbnail exists";
 } else {
     // create the thumbnail directory for writing
     create_directory($full_ftp_path, $thumbnail_dir, $path_to_images, $ftp_hostname, $ftp_username, $ftp_password);
     // location of the original image
     $sourcefile = "{$path_to_images}/{$item['0']}";
     // output file
     $targetfile = $thumbnail;
     /* Create a new image object (not neccessarily true colour) */
     $source_id = null;
     // prep according to image type
     if (preg_match('/(jpg|jpeg)$/i', $sourcefile)) {
         $source_id = imageCreatefromjpeg("{$sourcefile}");
     } elseif (preg_match('/(png)$/i', $sourcefile)) {
         $source_id = imageCreatefrompng("{$sourcefile}");
     } elseif (preg_match('/(gif)$/i', $sourcefile)) {
         $source_id = imageCreatefromgif("{$sourcefile}");
     } else {
         die("Unknown image file type");
     }
     /* Get the dimensions of the source picture */
     $source_width = imagesx($source_id);
     $source_height = imagesy($source_id);
     // scale or crop during thumbnail resize
     $scale = max($thumbnail_max_width / $source_width, $thumbnail_max_height / $source_height);
     if ($scale < 1) {
         $thumbnail_actual_width = floor($scale * $source_width);
         $thumbnail_actual_height = floor($scale * $source_height);
         $target_id = imagecreatetruecolor($thumbnail_actual_width, $thumbnail_actual_height);
         if (function_exists('imagecopyresampled')) {
function resize($path, $size)
{
    $array = getimagesize($path);
    $width_orig = $array[0];
    $height_orig = $array[1];
    if ($width_orig <= $height_orig) {
        $nwidth = $size * $width_orig / $height_orig;
        $nheight = $size;
    } else {
        $nwidth = $size;
        $nheight = $size * $height_orig / $width_orig;
    }
    $nwidth = intval($nwidth);
    $nheight = intval($nheight);
    $image_p = imagecreatetruecolor($nwidth, $nheight);
    switch ($array[2]) {
        case 1:
        case 2:
            $image = imageCreatefromjpeg($path);
            break;
        case 3:
            $image = imageCreatefrompng($path);
            break;
        default:
            return FALSE;
            break;
    }
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nwidth, $nheight, $width_orig, $height_orig);
    switch ($array[2]) {
        case 1:
        case 2:
            imagejpeg($image_p, $path, 80);
            break;
        case 3:
            imagepng($image_p, $path);
        default:
            return FALSE;
            break;
    }
}
Exemple #4
0
 private function getImg($name, $imgInfo)
 {
     $srcPic = $this->path . $name;
     switch ($imgInfo["type"]) {
         case 1:
             //gif
             $img = imagecreatefromgif($srcPic);
             break;
         case 2:
             //jpg
             $img = imageCreatefromjpeg($srcPic);
             break;
         case 3:
             //png
             $img = imageCreatefrompng($srcPic);
             break;
         default:
             return false;
     }
     return $img;
 }