如何制作不同尺寸/尺寸的单个图像

Posted

技术标签:

【中文标题】如何制作不同尺寸/尺寸的单个图像【英文标题】:How to make single image in different dimentions / size 【发布时间】:2017-03-25 06:48:17 【问题描述】:

实际上,我想要这样,当我上传 1 张图片时,原始图片将存储在 upload/og/ 中。但我也想要不同尺寸的图像,如1366x7681280*600768x1024 等......不仅仅是这些尺寸,它将与该图像成比例。

我有一个代码,可以将该图像转换为带有比例的拇指,这适用于max-width=300max-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'>&times;</a></div>";

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

【问题讨论】:

你有没有试过把这个函数放在一个循环中,以适当的大小作为参数,它应该按照你的要求做。 对不起,兄弟。我对php没有足够的了解。但我想制作像hdwallpapers.in/2017_wonder_woman_movie-wallpapers.html 这样的网站 【参考方案1】:

正如评论的那样,您实际上只需将最后一部分(函数)放在一个循环中,并使用mkdir() 添加一些目录创建逻辑。还有一个本机函数可以提取名为pathinfo() 的扩展名,因此只需使用该扩展名即可:

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

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

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 '<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    =   __DIR__."/uploads/scrnsots/".$image_name;
                $basepath   =   pathinfo($newname,PATHINFO_DIRNAME);
                # Create the folder if not already made
                if(!is_dir($basepath))
                    mkdir($basepath,0755,true);
                # It's more common to use this function here instead of copy()
                if (!move_uploaded_file($_FILES['scrnsots']['tmp_name'], $newname)) 
                    echo '<h1>Copy unsuccessfull!</h1>';
                    $errors=1;
                
                else 
                    # Have your sizes here
                    $sizes  =   array(
                        array(1366,768),
                        array(1280,600),
                        array(768,1024)
                    );
                    # Loop through your sizes here and apply your function
                    foreach($sizes as $hw) 
                        $thumb_name =   __DIR__.'/uploads/scrnsots/thumb/'.$hw[0].$hw[1].'/thumb-'.$image_name;
                        $thumb_path =   pathinfo($thumb_name,PATHINFO_DIRNAME);
                        # Create the thumbnail directory(s) if not exist
                        if(!is_dir($thumb_path))
                            mkdir($thumb_path,0755,true);
                        # Run your function
                        make_thumb($newname,$thumb_name,$hw[0],$hw[1]);
                    
                
            
        
    

【讨论】:

【参考方案2】:

经过这么多尝试,我找到了答案。通过更改以下行中的一些内容

    else
    
        $thumb_name='uploads/scrnsots/thumb/thumb-'.$image_name;
        $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);
    

    else
    
        $cars = array("300", "800", "1280","1366","1920");
        $arrlength = count($cars);
        for($x = 0; $x < $arrlength; $x++) 
        echo $cars[$x];
        $thumb_name='uploads/scrnsots/'.$cars[$x].'/thumb-'.$image_name_dash;
        $thumb=make_thumb($newname,$thumb_name,$cars[$x],$cars[$x]);
            list($width_main, $height_main, $type, $attr) = getimagesize($newname);
            list($og_image_w, $og_image_h, $type, $attr) = getimagesize("$thumb_name");
            echo "<br>";
        
    

它有效。完美。 这个想法来自 http://www.w3schools.com/php/php_arrays.asp 我只是在名为"300", "800", "1280","1366","1920" 的目标路径中创建了 5 个文件夹。

然后,它完美地在特定文件夹中生成特定图像。 说真的,我很高兴。

【讨论】:

以上是关于如何制作不同尺寸/尺寸的单个图像的主要内容,如果未能解决你的问题,请参考以下文章

屏幕尺寸分辨率问题

如何使用c ++为最小尺寸的窗口制作屏幕截图

如何创建具有 GridBagLayout 单元格精确尺寸的 ImageIcon

如何裁剪 W/H 相同尺寸的图像(Ubuntu 16)[关闭]

如何使用 jcrop 裁剪不同尺寸的图像

如何使用c ++为最小尺寸的窗口制作屏幕截图