Jump to content

[SOLVED] Thumbnail Generator


wayne0126

Recommended Posts

Try the forum-search. We got that topic many times.

 

<?php
function resizeimage($file) {
	if(file_exists("images/".$file)) {
		$image		= imagecreatefromjpeg("images/".$file); //unresized image
		$im_info	= getimagesize("images/".$file); //unresized image
		$im_width	= $im_info[0];
		$im_height	= $im_info[1];
		$im_flag	= $im_info[2];

		//max height: 100px; max width: 100px
		if($im_width >= $im_height) {
			$im_divice	= $im_width / 100;
		}else {
			$im_divice	= $im_height / 100;
		}
		$thumb_width	= $im_width / $im_divice;
		$thumb_height	= $im_height / $im_divice;

		//create empty image
		$thumb	= imagecreatetruecolor($thumb_width, $thumb_height);
		//resize image
		imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

		if(imagejpeg($thumb, "thumbnails/".$file, 80)) { //image-resource, filename, quality
			return 1;
		}

		imagedestroy($thumb); //destroy temporary image-resource
		imagedestroy($image); //destroy temporary image-resource
	}
}
?>

make 4 folders

 

pic

pic1

pic2

pic3

 

<?php

// this our dedicated function that creates our resized images
function createImage($width, $height, $srcimg, $img_thumb)
{
    $srcImg = ImageCreateTrueColor( $width, $height ) or die('Problem In Creating image');

    if( function_exists('imagecopyresampled' ))
    {
        imagecopyresampled( $srcImg, $srcimg, 0, 0, 0, 0, $width, $height, ImageSX($srcimg), ImageSY($srcimg) ) or die('Problem In resizing img #' . $key);
    }
    else
    {
        Imagecopyresized( $srcImg, $srcimg, 0, 0, 0, 0, $width, $height, ImageSX($srcimg), ImageSY($srcimg) ) or die('Problem In resizing img #' . $key);
    }

    ImageJPEG( $srcImg, $img_thumb, 90 ) or die('Problem In saving img');

    imagedestroy( $srcImg );
}

if(isset($_POST['Submit']))
{
    $size[1] = 100;
    $size[2] = 200;
    $size[3] = 300;

    $filedir = './pics/';

    $thumbdir[1] = './pics1/';
    $thumbdir[2] = './pics2/';
    $thumbdir[3] = './pics3/';

    $prefix = '00005';
    $maxfile = '2000000';
    $mode = '0666';

    $userfile_name = $_FILES['image']['name'];
    $userfile_tmp  = $_FILES['image']['tmp_name'];
    $userfile_size = $_FILES['image']['size'];
    $userfile_type = $_FILES['image']['type'];

    if (isset($_FILES['image']['name']))
    {
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb[1] = $thumbdir[1].$prefix.$userfile_name;
        $prod_img_thumb[2] = $thumbdir[2].$prefix.$userfile_name;
        $prod_img_thumb[3] = $thumbdir[3].$prefix.$userfile_name;

        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        $srcimg = ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');

        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if (($sizes[1] <= $size[1]) || ($sizes[1] <= $size[2]) || ($sizes[1] <= $size[3]))
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }
        else
        {
            $new_height[1] = $size[1];
            $new_height[2] = $size[2];
            $new_height[3] = $size[3];

            $new_width[1] = abs($new_height[1]/$aspect_ratio);
            $new_width[2] = abs($new_height[2]/$aspect_ratio);
            $new_width[3] = abs($new_height[3]/$aspect_ratio);
        }

        // loop through each img width and call a dedicated function to
        // create different sizes of the uploaded image
        foreach($new_width as $cur_img => $cur_img_width)
        {
            // this function uses the variables set above
            createImage($cur_img_width, $new_height[$cur_img], $srcimg, $prod_img_thumb[$cur_img]);
        }
    }

    /*echo '<a href="'.$prod_img.'">
    <img src="'.$prod_img_thumb[3].'" width="'.$new_width[3].'" height="'.$new_height[3].'">
</a>';*/

// show the created images
    foreach($prod_img_thumb as $cur_img => $img_thumb)
    {
        echo '<h3>Img #' . $cur_img . '</h3>
<a href="' . $prod_img . '">
    <img src="'.$prod_img_thumb[$cur_img].'" width="'.$new_width[$cur_img].'" height="'.$new_height[$cur_img].'">
</a><br />
Dimensions: ' . $new_width[$cur_img] . 'px X ' . $new_height[$cur_img] . 'px';
    }
}
else
{
    echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
    <input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.