简体   繁体   English

创建映像并重命名不适用于PHP

[英]Create image and rename is not working in PHP

I need to create process for making images into different dimensions and move them into repective folder.The below code is working for some images and not for some.Some images are being renamed in subfolder and some are not.some images are being created and some are not.I have performed unit testing on single image and it works.But when i run this on server,i get above problem.There are more than 500 images which needs to be processed.I am not able to understand where i went wrong.Please help. 我需要创建将图像制作成不同尺寸的过程并将它们移动到相应的文件夹中。下面的代码适用于某些图像而不适用于某些图像。有些图像正在子文件夹中重命名,有些则不是。有些图像正在创建,有些图像正在创建是不是。我已经对单张图像进行了单元测试并且它有效。但是当我在服务器上运行时,我遇到了上述问题。有超过500张图像需要处理。我无法理解我哪里出错了。请帮忙。

    $arr=Array();
    $arr["popup"]=Array("width"=>228,"height"=>304);
    $arr["zoom"]=Array("width"=>1500,"height"=>2000);
    $source = imagecreatefromjpeg($file_path);      
    list($width, $height) = getimagesize($file_path);

    foreach($arr as $image_type=>$dimention){   
        echo "<br> $image_type : ";
        $temp_folder_name=$image_type;
                    $temp_folder_path='../prod_images/'.$folder_name.'/'.$temp_folder_name;

        //check if directory exists if not then create else traverse
        if (!file_exists($temp_folder_path)):
            mkdir($temp_folder_path);
        else:
            $files = glob($temp_folder_path.'/'.$filename);
            if (!empty($files))){
                echo "<br/>Images exists";
                $curr_dt = date('Ymd_His');
                $dt = '_'.$curr_dt;
                $file_arr = explode(".",$filename);
                $new_filename = $file_arr[0].$dt.'.'.$file_arr[1];

                $oldname = '../prod_images/'.$folder_name.'/'.$temp_folder_name.'/'.$filename;
                echo "<br>Old Name : ".$oldname;
                $newname = '../prod_images/'.$folder_name.'/'.$temp_folder_name.'/'.$new_filename;
                echo "<br>New Name : ".$newname;
                rename($oldname,$newname);
            }else{
                echo "<br/>Images does not exist";

            }
        endif;

        $curr_width=$dimention["width"];
        $curr_height=$dimention["height"];
        echo "<br> $image_type - $curr_width - $curr_height";
        $temp_image = imagecreatetruecolor($curr_width, $curr_height);
        //imagecopyresized($temp_image,$source, 0, 0, 0, 0, $curr_width, $curr_height, $width, $height);
        imagecopyresampled($temp_image,$source, 0, 0, 0, 0,$curr_width, $curr_height, $width, $height);         
        imagejpeg($temp_image,$temp_folder_path."/".$filename); 
        imagedestroy($temp_image);

According to me, your code seems ok. 据我说,你的代码似乎没问题。 You might need to check the rights you get on your files and folder. 您可能需要检查文件和文件夹的权限。 Simply type ls -l on one of your folder OR, I don't recommend you to sudo chmod 777 -R ./ but I think it is the best way to check if your server has the right to write and read into your /prod_images/ folder. 只需在你的一个文件夹上键入ls -l或者,我不建议你使用sudo chmod 777 -R ./但我认为这是检查你的服务器是否有权写入和读入/ prod_images的最佳方法/文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM