Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

php - how to write value on image through using excel , csv

I have a lot of names in excel sheet. I want to write them one by one on an image. Image is repeated. The same image shows one name. Please, tell me how to write code /manual /using Photoshop or other method. http://prntscr.com/kowpr4

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, I would recommend you export your spreadsheet to some open, non-Microsoft, non-proprietary format, such as a CSV file.

Let's assume it looks like this and is called "names.csv":

William, Gates
Kermit, Frog

Now, grab a copy of the indispensable ImageMagick and install it.

Now you need to generate a decent quality badge image rather than the horrible one you linked to. I made mine 1200x760 - so grab this one if you want things to align properly:

enter image description here

Then you will need a script to iterate through the list of names. I am doing it with bash but you can use Windows for loop, like here:

#!/bin/bash

counter=1
while IFS=', ' read first last; do
   filename="badge-$counter.jpg"
   echo Generating file $filename, for $first, $last

   magick -font ComicSansMSB -background none -fill magenta -size 420x60 caption:"$first" first.png
   magick -font Verdana      -background none -fill blue    -size 420x60 caption:"$last"  last.png
   magick badge.png first.png -geometry +710+360 first.png -composite last.png -geometry +710+460 -composite "$filename"
   ((counter+=1))
done < names.csv

So, for each line in the file, I read the first name and surname. I have a counter that increments for each file (i) so that I can generate a unique filename.

I then make a little image, sized to match the text field, called "first.png with the first name. Likewise for the surname, a file called "last.png". I do them in different fonts and colours just for fun and so you can see how to do it. I then load up the badge template, and after setting the correct position, composite the names onto the badges and save with unique filenames.

enter image description here

enter image description here


I have marked up an image so you can see where the dimensions come from:

enter image description here


Keywords: ImageMagick, form-filling, form completion, badge, name badge, automated, automatic, scripted, command-line, command line, Excel, CSV.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...