Tareq Alam

i have promises to keep and miles to go before i sleep – Robert Frost

PHP: Change Image Opacity June 20, 2008

Filed under: PHP — tareqalam @ 1:03 pm
Tags: , , ,

Here is a script which will change the image opacity…

Try this code and have fun..
$imageName = 'path/to/your/image/file'
$im_src = create_image_from_type($imageName);
$size = getimagesize($imageName);
$im_dst = create_image_from_type($imageName);
$white = imagecolorallocate($im_dst, 255, 255, 255);
imagecolortransparent($im_dst, $white);
imagefilledrectangle($im_dst, 0, 0, $size[0], $size[1], $white);
$opacityVal = 50;// put the opacity value here
imagecopymerge($im_dst, $im_src, 0, 0, 0, 0,$size[0], $size[1], $opacityVal);
save_image($im_dst, $imageName, 100);
imagedestroy($im_dst);
imagedestroy($im_src);

function save_image($image_dest,$image_src,$image_quality)
{
$image_type = getimagesize($image_src);
if($image_type['mime']=='image/png')
{
imagepng($image_dest,$image_src);
}
else
{
imagejpeg($image_dest,$image_src,$image_quality);
}
return true;
}

function create_image_from_type($image_path)
{
$image_type = getimagesize($image_path);
if($image_type['mime']=='image/png')
{
$im = imagecreatefrompng($image_path);
}
else
{
$im = imagecreatefromjpeg($image_path);
}
return $im;
}

 

pyExcelerator: Python exportar Excel to MySQL June 20, 2008

Filed under: ploneCMS, python — tareqalam @ 12:43 pm
Tags: , , ,

Hi all… here is the code to import the xls data into mysql using pyexcelerator and python.. About the pyexcelarator installation and configuration i have discussed an article… click this link to view that if you dont have any idea what is pyexclerator.. http://tareqalam.wordpress.com/2008/05/03/python-exportar-mysql-to-excel/

Now here is the python script which takes input a xls file and inserts the comment field it into the mysql table. basically it is helpful if you want to add comment to a mysql table.. I know there is some other easy way to do that.. but this code by making some customization you can insert data also.. Maybe my coding is not that rich.. but i am trying to help people.. if my code helps people a little i will be happy.. so hav e a look on the code…

import pyExcelerator
import sys
from pprint import pprint as pp

#put the table name here
table_name = 'test_table'
#put the xls file name here after modifying the commet fields
xls_file = 'test_table.xls'


sql_array = []
try:
workbook = pyExcelerator.parse_xls(xls_file)
except:
sys.exit('cant load %s '%xls_file)
worksheet = workbook[0]
# print cells A1 and B1
rowvalue= worksheet[1]
#pp(worksheet)
#sys.exit(1)
err_flag=0
#print t[(10,0)]
#print len(row_value.keys())
i = 1
for row_idx, col_idx in sorted(row_value.keys()):
if row_idx !=0:
err_flag=0
try:
MODIFY_FIELD_COMMENT = row_value[(i,0)]
except:
err_flag=1
try:
MODIFY_FIELD_NAME = row_value[(i,4)]
except:
err_flag=1
try:
MODIFY_FIELD_TYPE = row_value[(i,8)]
except:
err_flag=1

if err_flag == 0:
sql_for_insert_data_into_table="alter table %s modify %s %s COMMENT '%s'"%(table_name,MODIFY_FIELD_NAME,MODIFY_FIELD_TYPE,MODIFY_FIELD_COMMENT)
sql_array.append(sql_for_insert_data_into_table)
i=i+1


#print sql_array

#now inserting comments are done here

import MySQLdb

conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='test_db')
cu = conn.cursor()
for insert_row_sql in sql_array:
try:
cu.execute(insert_row_sql)
except MySQLdb.Error, e:
errInsertSql = "Insert Sql ERROR!! sql is==>%s" %(insert_row_sql)
print errInsertSql
#sys.exit(errInsertSql)
continue
print 'done!! see in the database the %s table field\'s comment'%table_name
#pp(worksheet)

Pleas contact me if you are unable to understand any thing.. i ll try to help you ..

Thanks…