473,594 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic generation of images (was Re: Custom Classes?)

On Thu, 2008-05-08 at 10:33 -0500, Victor Subervi wrote:
Okay, trying this again with everything working and no ValueError or
any other errors, here we go:

Load this code. Unless you use a similar login() script, you will want
to edit your own values into the user, passwd, db and host:

#!/usr/bin/python

import MySQLdb

import sys,os
sys.path.append (os.getcwd())
from login import login
user, passwd, db, host = login()

pic = "pic1"
w = 20
x = 0
d = 6
y = 1

getpic = "getpic" + str(w) + ".py"
try:
os.remove(getpi c)
except:
pass
code = """
#!/usr/local/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append (os.getcwd())
from login import login
user, passwd, db, host = login()
form = cgi.FieldStorag e()
picid = int(form["id"].value)
x = int(form["x"].value)
pics =
{1:'pic1',2:'pi c1_thumb',3:'pi c2',4:'pic2_thu mb',5:'pic3',6: 'pic3_thumb',7: 'pic4',8:'pic4_ thumb',\
9:'pic5',10:'pi c5_thumb',11:'p ic6',12:'pic6_t humb'}
pic = pics[x]
db = MySQLdb.connect (host, user, passwd, db)
cursor= db.cursor()
sql = "select %s from products where id='%s';" % (pic, str(picid))

cursor.execute( sql)
content = cursor.fetchall ()[0][0].tostring()
cursor.close()
print 'Content-Type: image/jpeg'
print
print content
"""
script = open(getpic, "w")
script.write(co de)
script.close()

and then surf to:
http://whatever.url/getpic20.py?id=6&x=1
Also, please re-send the link on how to post good questions to the
list. I cannot find it.
TIA,
Victor
Why are you dynamically creating getpic20.py?

Obviously, in the real script, you probably have several of them:
getpic1.py, getpic2.py, etc., but is there any reason you couldn't just
have one getpic.py, and pass the number in as a parameter in your GET
request, like this:

http://localhost/getpic.py?id=6&x=1&w=20

Then you can just have a static getpic.py. I've stripped out useless
bits:

* You don't use sys or os, so why import them?
* MySQL makes it difficult for me to replicate your behavior, because I
don't have your DB setup. The DB call has been replaced with a
dictionary of keys into JPG data, pulled from the filesystem. Change
filenames to jpegs on your own hard drive
* login is useless without MySQL.

So your new getpic.py (statically created) looks like this:

~~~ getpic.py ~~~
#!/usr/local/bin/python

import cgitb; cgitb.enable()
import cgi

pics = {
1: open('pic.jpg') .read(),
2: open('pic2.jpg' ).read()
}
# you could just pass the filename, and then you wouldn't have to load
the
# image data in advance, but this more closely mimics the concept of
your DB
# setup.

form = cgi.FieldStorag e()
x = int(form["x"].value)
pic = pics[x]

print 'Content-Type: image/jpeg'
print
print pic
~~~

Then if you want to include your pictures in a web page, you do

~~~ show_pics.html ~~~
<html>
<head><title>Pi ctures</title></head>
<body>
<h1>pictures</h1>
<img src='http://example.com/getpic.py?x=1'/>
<img src='http://example.com/getpic.py?x=2'/>
</body>
</html>
~~~

This should work just as well when you refactor the script to pull from
the database.

Hope this helps. Also, the article on asking good questions is
available here: http://catb.org/~esr/faqs/smart-questions.html

Jun 27 '08 #1
0 874

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
5235
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
1
2044
by: duncan.lovett | last post by:
I am working on a graphical heading generator for a clients website as their server does not have the GD library or similar plugins for dynamic image generation. I have achieved the result partly, from a snipet I found in the user contributions to the "str_replace" function in the manual on php.net. However there is a small problem - it only works in uppercase. Please first see my code below and then a brief description of the problem:
7
1481
by: Andrew Robinson | last post by:
Given HTML text (likely from SQL), is there any method that could be employed to render server and/or custom controls that are contained inside of that text? I would be loading content from a database and would like to 'expand' the server controls. Possibly even recursively. Hope this makes sense, but I am guessing the answer is no. If this is something that can't be done, I am looking at creating my own markup tags and searching for...
3
13734
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
0
5277
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all the days in the week with dates and textboxes to fill in some data. row 1: question
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8231
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.