実際には、私が望むのは、1枚の画像をアップロードすると元画像はupload/og/
に格納されます。しかし、私はまた、1366x768
、1280*600
、768x1024
などのような異なるサイズの画像を望んでいます...これらの寸法だけではなく、その画像の比に比例します。異なる大きさ/サイズの単一画像を作る方法
この画像を比率でサムに変換するコードは1つありますが、これはmax-width=300
とmax-height=600
です。
define ("MAX_SIZE","100");
define ("WIDTH","300");
define ("HEIGHT","600");
function make_thumb($img_name,$filename,$new_w,$new_h)
{
$ext=getExtension($img_name);
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);
//gets the dimmensions of the image
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// next we will calculate the new dimmensions for the thumbnail image
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // resize the big image to the new created one
if(!strcmp("png",$ext)) // output the created image to the file. Now we will have the thumbnail into the file named by $filename
imagepng($dst_img,$filename);
else
imagejpeg($dst_img,$filename);
imagedestroy($dst_img);
imagedestroy($src_img);
}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['scrnsots']['name'];
if ($image)
{
$filename = stripslashes($_FILES['scrnsots']['name']);
// get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
// if it is not a known extension, we will suppose it is an error, print an error message
//and will not upload the file, otherwise we continue
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png"))
{
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
$size=getimagesize($_FILES['scrnsots']['tmp_name']);
$sizekb=filesize($_FILES['scrnsots']['tmp_name']);
if ($sizekb > MAX_SIZE*102400)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$image_name= $image .''. time() .'.'.$extension;
$newname="uploads/scrnsots/".$image_name;
$copied = copy($_FILES['scrnsots']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}
else
{
$thumb_name='uploads/scrnsots/thumb/thumb-'.$image_name;
$thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);
}
}
}
}
if(isset($_POST['submit']) && !$errors)
{
echo $thumb_name ."<br/>";
echo "<div class='custom alert alert-success'>Successfully Added.<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a></div>";
}
ただし、親指だけが作成されます。私は異なった次元(高さ - 幅)の1つのイメージがほしい。
この関数をパラメータとして適切なサイズのループに入れようとしましたが、これは必要なときに行う必要があります。 – Rasclatt
申し訳ありません。私はPHPについて十分な知識を持っていません。しかし、私はhttp://www.hdwallpapers.in/2017_wonder_woman_movie-wallpapers.htmlのようなウェブサイトを作りたい –