如何新建php页面大小,php – 如何制作不同尺寸/大小的单个图像

实际上,我想要的是,当我上传1张图片时,原始图片将存储在upload / og /中.但我也想要不同尺寸的图像,如1366×768,1280 * 600,768×1024等…不仅仅是这些尺寸,它将与图像成比例.

我有一个代码,它将该图像转换为拇指与比率,这适用于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,$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 '

但它只会创建拇指.我想要1个不同的图像 – 尺寸(高度 – 宽度).

最佳答案

如评论所述,您实际上只需将最后一部分(函数)放在循环中,并使用mkdir()添加一些目录创建逻辑.还有一个本机函数来提取名为pathinfo()的扩展,所以只使用那个而不是你拥有的那个:

function make_thumb($img_name,$new_h)

{

$ext=getExtension($img_name);

if(!strcmp("jpg",$ext))

$src_img=imagecreatefromjpeg($img_name);

if(!strcmp("png",$ext))

$src_img=imagecreatefrompng($img_name);

$old_x=imageSX($src_img);

$old_y=imageSY($src_img);

$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,$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);

}

if(!empty($_FILES)) {

$errors=0;

if($_POST['submit']) {

if($_FILES['scrnsots']['error'] == 0) {

$image = $_FILES['scrnsots']['name'];

$filename = stripslashes($_FILES['scrnsots']['name']);

$extension = pathinfo($filename,PATHINFO_EXTENSION);

$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) {

echo '

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值