473,608 Members | 2,264 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 875

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
2046
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
1484
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
13735
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
8063
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8002
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
8496
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...
0
8475
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8338
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...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
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...
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.