0

I'm importing images from another script to my script.
Both of the images names are saved in the database.

My Script

Post_ID  -   old_pic_name           -   new_pic_name    -   is_gif
1        -   long_file_name.jpg     -   1.jpg           -     0
2        -   long_file_name2.png    -   2.png           -     0
3        -   long_file_name3.gif    -   3.gif           -     1

I have dedicated server. I've tried to make that conversion using php to import the old name from filename from the database then resize and rename it. And it was successful but had a small issue.

The original website contains more than 116000 images. PHP converting was taking about 15 minutes to convert 500 photo and sometimes ends with errors and I had to restart refresh manually.

I don't know if I can't give the script all the server resources to finish the loading faster, or there's something else.

I want to do it using SHELL. May be it will be faster and it will override php max_execution_time and memory_limit limits.

The original photos are located in /old_data/ folder. The new folders should be like that : /o/ contains original copy of the file named with new_post_id.image_extension
/t/ contains 3 resized versions of the (728px width) name l-new_post_id.image_extension and (500px width) new_post_id.image_extension and s-new_post_id.image_extension (300px width).

For example:
First row:
ID = 1
Old Photo dir: /old_data/long_file_name.jpg
New Photo dir: /o/1.jpg (Exact copy of the original, just renamed)
New Resized Photos: /t/1.jpg (500px max-width) & /t/l-1.jpg (728px max-width) & /t/s-1.jpg (300px max-width)

I was thinking if I could export these information into a suitable file format that can be imported using bash shell in linux and use the data to do the renaming and resizing recursively using GD and ImageMagick. But honestly, I have no clue how to do that.

I hope someone can help.

Best Regards.

2 Answers2

0

Okay man, I've tried editing your stuff, and still I can't understand.

What I know about programming is : you have to be able to spell what you are doing the simplest way you can. Then you can actually start coding.

So, I'd say, think small to begin. Cut your work into parts. Think iterative and incremental.

Also, if you want help from stackoverflow, think about formatting your question so that people can actually understand it.

This could have been a comment, but really, it's an answer.

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • Thanks very much for the editing. I have edited the post again to explain it more after I modified my database. May be it's clearer now. Thanks for the attention. – Mostafa Shoman Aug 13 '15 at 01:51
0

The question is not that clear, but anyway I'll try to give an answer that could help.

As I understand from your question you have a PHP-script that pulls over 100.000 images from a server and puts them in a directory named old_data/. Then you want a shell/bash script that takes all images listed in a file that are in this directory and resizes them. The results will be stored in three different directories.

I assume you create the file where the images are listed in your PHP-script and can therefore choose the formatting of this file yourself; I would recommend to store one image per line of this file. I would store it like this:

1;long_file_name.jpg
2;some_other_file.png
...

you can also store it delimited by a tabulator or a space or similar. I would not store the target directories in this line as they are same for every image.

Now what you need is a shell script, that iterates over the lines of this file and resizes all images putting them in the three target directories. I would use convert to resize the images. convert can be used like this:

convert old_data/long_file_name.jpg -resize 300 /t/s-1.jpg

All we have to do now is to embed this command into a script.

while read line || [[ -n "$line" ]]; do
  num= echo $line | cut -d ';' -f 1
  file= echo $line | cut -d ';' -f 2
  fe= ${file##*.}
  mv old_data/${file} o/${num}.${fe}
  convert old_data/${file} -resize 500 /t/${num}.${fe}
  convert old_data/${file} -resize 728 /t/l-${num}.${fe}
  convert old_data/${file} -resize 300 /t/s-${num}.${fe}
done < $1

To run this script use:

./script image_list

Note that the || [[ -n "$line" ]] is just to ensure that the script reads the last line of the file also if it isn't followed by a linefeed; if your PHP-sript adds a linefeed at the end of the file you can erase this part.
Also you can try to convert your image to 728px one,then convert this to the 500px one, and finally convert the 500px to 300px to gain a possible speedup by reading smaller files, but I'm not sure about this, you have to try it.

I'm actually not sure if you have to escape the dot in ${file##*.}, as in my bash there work both: ${file##*.} and ${file##*\.}.

To speed up the converting, have a look here, there might be some useful information about parallelizing the whole thing.

I have taken some code for this answer from cppcoder, here and from Stephen, here.

Community
  • 1
  • 1
Klaus
  • 538
  • 8
  • 26