function CreateThumb($file, $maxwdt, $maxhgt, $dest, $quality = 1) { if (!file_exists($file)) { return FALSE; } if (setmemoryforimage($file) === FALSE) { return FALSE; } list($owdt, $ohgt, $otype) = getimagesize($file); if ($owdt < $maxwdt) { $maxwdt = $owdt; } if ($ohgt < $maxhgt) { $maxhgt = $ohgt; } if ($owdt <= $maxwdt && $ohgt <= $maxhgt) { @copy($file, $dest); chmod($dest, 420); return TRUE; } switch ($otype) { case 1: $newimg = imagecreatefromgif($file); break; case 2: $newimg = imagecreatefromjpeg($file); break; case 3: $newimg = imagecreatefrompng($file); break; case 6: case 15: $newimg = @imagecreatefromwbmp($file); break; return FALSE; } if (!$newimg) { return FALSE; } if (1500 < $owdt || 1200 < $ohgt) { list($owdt, $ohgt) = resample($newimg, $owdt, $ohgt, 1024, 768, 0); } resample($newimg, $owdt, $ohgt, $maxwdt, $maxhgt, $quality); if (!$dest) { return $newimg; } if (!is_dir(dirname($dest))) { mkdir(dirname($dest)); } switch ($otype) { case 1: imagegif($newimg, $dest); break; case 2: imagejpeg($newimg, $dest, 90); break; case 3: imagepng($newimg, $dest); break; case 6: case 15: @imagewbmp($newimg, $dest); } imagedestroy($newimg); chmod($dest, 420); return TRUE; }
<?php // header("Content-type: image/jpeg"); for ($i=0; $i <count(scandir('./'))-6 ; $i++) { // echo scandir('./')[$i+3]; echo 'begin'; resample(scandir('./')[$i+3],'./resize/'.$i.'.jpeg'); echo 'end'; // sleep(200000); } //重新裁图http://php.net/manual/zh/function.imagecopyresampled.php function resample($filename,$filerename){ /* 读取图档 */ $im = imagecreatefrompng($filename); /* 图片要截多少, 长/宽 */ $start_width=mt_rand(300,360); $start_length=mt_rand(300,400); $new_img_width = mt_rand(250,350); $new_img_height = mt_rand(400,800); /* 先建立一个 新的空白图档 */ $newim = imagecreate($new_img_width, $new_img_height); // 输出图要从哪边开始 x, y ,原始图要从哪边开始 x, y ,要画多大 x, y(resize) , 要抓多大 x, y $re=imagecopyresampled($newim, $im, 0, 0,$start_width , $start_length, $new_img_width, $new_img_height, $new_img_width, $new_img_height); /* 放大成 500 x 500 的图 */ // imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); /* 将图印出来 */ imagejpeg($newim,$filerename);