RsD0p9BK
2/1/2016 - 10:41 AM

createThumb.php

// создание миниатюр
function createThumb($file, $thumbWidth, $quality=80) { 

	// проверить путь
	if (file_exists(conf::$pref_include . $file)) {
		$path = conf::$pref_include;
	}
	else if (file_exists($file)) {
		$path = '';
	}
	else {
		return $file;
	}

	$thumbDir = dirname($file) . '/thumb/';
	$thumbFile = $thumbDir . basename($file);
	mkdir($path . $thumbDir, 0777, true);

	// создать миниатюру
	if (!file_exists($path . $thumbFile)) {

		$details = getimagesize("$path$file") or die('Please only upload images.'); 

		$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); 
		eval('$srcImg = imagecreatefrom'.$type.'("$path$file");'); 

		$thumbHeight = $details[1] * ($thumbWidth / $details[0]); 
		$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); 
		imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); 

		eval('image'.$type.'($thumbImg, "$path$thumbFile"'.(in_array($type, ['jpg', 'jpeg'])?', $quality':'').');'); 

		imagedestroy($srcImg); 
		imagedestroy($thumbImg); 
	}

	// вернуть миниатюру, если она создана
	if (file_exists($path . $thumbFile)) {
		return $thumbFile;
	} else {
		return $file;
	}
} 
// create image thumbnail

function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ 
    $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); 
    $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); 
    eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); 
    $thumbHeight = $details[1] * ($thumbWidth / $details[0]); 
    $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); 
    imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,  
    $details[0], $details[1]); 
    eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. 
    (($type=='jpeg')?', $quality':'').');'); 
    imagedestroy($srcImg); 
    imagedestroy($thumbImg); 
} 

foreach ($_FILES["pictures"]["error"] as $key => $error) { 
   if ($error == UPLOAD_ERR_OK) { 
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; 
       $name = $_FILES["pictures"]["name"][$key]; 
       move_uploaded_file($tmp_name, "data/$name"); 
       createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80); 
       //120 = thumb width  ::  80 = thumb quality (1-100) 
   } 
} 

// http://phpsnips.com/5/Create-Image-Thumbnail-On-Upload#.Vq8dczZ96S4