Which way to save images to a server?

2

I know the answer to this question is not accurate, but I am asking because of the alerts I received from a client hosting (DV medium)

The system I developed saves the images as follows:

/uploads/nome-do-modulo/ano/mes/dai/nome-imagem-encriptado.ext

The media temple sent me an alert saying I have many images in a single folder .

Really

My host config

  • Ubuntu 12.04 server
  • Apache 2.4.x
  • PHP 5.5.x

Sorry for my english.

If you need more information, tell me.

    
asked by anonymous 07.07.2015 / 14:16

2 answers

5

I use a technique I learned from the structure of an EC platform called PrestaShop.

It's a bit complicated to explain, so I'll show you an example. Even because I do not know if there is an appropriate term to define this technique.

For images of a product whose ID is 150, the images would fall within the following structure

/images/products/1/5/0/ddshdfhiioiuo.jpg
/images/products/1/5/0/eyfghioiuor.jpg
/images/products/1/5/0/aytdklkkoiuo.jpg

For the name of the images, I use a script that generates unique strings, in order to avoid conflict with existing files. But this is something personal, you can opt for other methods. Most important here is the structure where images are stored.

If I want to generate images of different dimensions, example 150px, 750px, 350px, it would look like this:

Taking the first image from the list above as an example:

/images/products/1/5/0/ddshdfhiioiuo.jpg (essa é a original, sem compressão, filtros, etc)
/images/products/1/5/0/ddshdfhiioiuo_150.jpg (essa é a de 150 px)
/images/products/1/5/0/ddshdfhiioiuo_750.jpg (essa é a de 750 px)
/images/products/1/5/0/ddshdfhiioiuo_350.jpg (essa é a de 350 px)

The intent of keeping the original is to be able to generate other images with different dimensions if necessary.

Why did your host ask you to reduce the number of images in one folder? Obviously because it gets too heavy. Imagine a small ecommerce of 50 thousand products, each of which has 5 images with 3 or 4 versions of each image. When you enter this folder through FTP it is a terror. A nightmare, especially for the host server.

Let's learn something interesting and very simple with PHP

How to generate a images / products / 1/5/0 directory from the number 150?

/**
Aqui é definido a base do diretório.
Evite usar paths relativos. Utilize sempre paths absolutos.
*/
$path = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'images/products' . DIRECTORY_SEPARATOR;

/**
O id do produto
*/
$item_id = 150;

/**
Cada caracter será convertido em valores de índices num array
*/
$arr = str_split( $item_id );

/**
Converte o array numa string onde cada valor é separado por DIRECTORY_SEPARATOR

Exemplo, 150 ficará como 1/5/0 ou 1
/images/products/1/5/0/ddshdfhiioiuo.jpg
/images/products/1/5/0/eyfghioiuor.jpg
/images/products/1/5/0/aytdklkkoiuo.jpg
*/ $folder = implode( DIRECTORY_SEPARATOR, $arr ); /** Concatena a base com o folder, formando o path absoluto final */ $path .= $folder . DIRECTORY_SEPARATOR; /** Descarta os objetos que não serão mais usados. Isso é útil somente para micro otimizações. */ unset( $arr, $folder ); /** Verifica se o path já não existe. Caso não existir, prossegue a operação */ if( !file_exists( $path ) ) { /** A função mkdir faz a mágica com ajuda do terceiro parâmetro. Quando o terceiro parâmetro receber (bool)TRUE, indica que os subfolders são criados recursivamente. Portanto, não precisa se preocupar em fazer laços de repetição para criar os subfolders. */ if( !mkdir( $path, 0777, true ) ) { /** * Se cair aqui, houve algum erro. É aconselhável retornar códigos de erro O código abaixo é meramente didático, para exemplo. */ $error_code = 303; } }
    
07.07.2015 / 14:48
1

Normally I prefer to convert the images to a base64 string and save them to the database with the name and source in an image table.

    
08.07.2015 / 08:54