简体   繁体   English

通过重命名的PHP传递文件

[英]pass files through php with rename

I have 1600 images, each 256px. 我有1600张图片,每张256像素。 These images have been sliced in photoshop from an image that is 10240px x 10240px in to the tiles. 这些图像已经在Photoshop中从10240像素x 10240像素的图像切片到瓷砖。 Problem is, photoshop has named them image_0001.png, image_0002.png... 问题是,photoshop将它们命名为image_0001.png,image_0002.png ......

I would like to rename these in to a usable file name such as image_x_y.png x being tile number in that row, y being tile number in that column... 我想将这些重命名为可用的文件名,例如image_x_y.png x是该行中的tile号,y是该列中的tile号...

And ideas how i can automate the renaming of these, or if not how i can pass these images through php, so that i can access image.php?x=2&y=1 ect... 和想法如何我可以自动重命名这些,或者如果不是我如何通过PHP传递这些图像,以便我可以访问image.php?x = 2&y = 1等...

thanks in advance 提前致谢

EDIT: I have no permission to answer my own question but, used this as renaming would be required on every update. 编辑:我没有权限回答我自己的问题但是,在每次更新时都需要重命名。 Not ideal... 不理想......

<?php
$x=$_GET['x'];
$y=$_GET['y'];
$image=(($y-1)*40)+$x;
if ($image<10){
$image="0".$image;
}
$url="tiles/" . $image . ".jpg";

header("Location:" . $url);
?>

You could open the directory containing your files and then create a loop to access all images and rename them, like: 您可以打开包含文件的目录,然后创建一个循环来访问所有图像并重命名它们,如:

<?php

if ($handle = opendir('/path/to/image/directory')) {
    while (false !== ($fileName = readdir($handle))) {
        //do the renaming here
        //$newName = 
        rename($fileName, $newName);
    }
    closedir($handle);
}
?>

Useful functions: 有用的功能:

rename() , readdir() , readdir() , str_replace() , preg_replace() rename()readdir()readdir()str_replace()preg_replace()

Hope this helps! 希望这可以帮助!

You don't have to rename them, just calculate the "linear id" on every access. 您不必重命名它们,只需在每次访问时计算“线性ID”。

so, assuming you have a 40 * 40 set of files, in image.php you'd have something like 所以,假设你有一个40 * 40的文件集,在image.php你会有类似的东西

$fileid = $x * 40 + y;
$filename = sprintf("image_%04d.png",$fileid);
// send the file with name $filename

What formula you need depends on how it was sliced, could as well be $y * 40 + x 你需要什么样的公式取决于切片的方式,也可以是$y * 40 + x

The main advantage is that should your image be updated, it will be ready to use without the intermediate step of renaming the files. 主要优点是,如果您的图像被更新,它将可以在没有重命名文件的中间步骤的情况下使用。

Try this : 尝试这个 :

$dir = "your_dir";
$i = 0;
$j = 0;
$col = 5;
foreach(glob($dir . '/*') as $file) 
{ 
    rename($file, "image"."_".$j."_".$i);
    $i++;
    if($i % $col == 0)
    {
        $j++;
    }
} 

If you know for sure that the routine that converted your files always named the resulting images in the same order ie top left = 0001 ...... Bottom right = 0016 如果您确定转换文件的例程总是以相同的顺序命名结果图像,即左上角= 0001 ......右下= 0016

Then it should be fairly simple to write a quick CLI script to go through and rename all your images. 然后编写一个快速的CLI脚本来完成并重命名所有图像应该相当简单。

Alternatively if you are going to use that same image converter again many times it may be simpler to make your image.php?x=1&y=2 script workout what file to serve then you wont need to do the renaming every time you get new images. 或者,如果你要再次使用相同的图像转换器,那么制作你的image.php可能更简单吗?x = 1&y = 2脚本锻炼要服务的文件然后你每次获得新图像时都不需要重命名。

-read the soure folder with your images ( http://php.net/manual/de/function.readdir.php ) - 使用您的图像读取soure文件夹( http://php.net/manual/de/function.readdir.php

-put the part of each imagename between "_" and "." - 在“_”和“。”之间输入每个图像名称的一部分。

-parse it ($image_nr) as integer -parse它($ image_nr)为整数

-do the following: -请执行下列操作:

$y = floor($image_nr/40);
$x = $image_nr%40;

finaly put each image in your destination directory with the new name 最终使用新名称将每个图像放在目标目录中

I Have not tested it but you could try using this: 我没有测试过,但您可以尝试使用它:

$imagesInARow = 10240/256; //=> 40
$rows = 1600 / $imagesInARow; //=> 40

$imageIndex = 1;
for($i = 1; $i <= $rows; $i++) { // row iteration
    for($j = 1; $j <= $imagesInARow; $j++) { // columns iteration
        rename('image_'. str_pad($imageIndex, 4, '0', STR_PAD_LEFT).'.png',
               "image_{$i}_{$j}.png");
        $imageIndex ++;
    }
}

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

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