PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ales/echo/png_test.php

http://cartonbank.googlecode.com/
PHP | 655 lines | 459 code | 115 blank | 81 comment | 41 complexity | a741d1c10f86cfb2c9ba8ee75a117b59 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?
  2. // generate images for echo spb
  3. //
  4. // configuration
  5. include("/home/www/cb3/ales/config.php");
  6. //open db connection
  7. $link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
  8. mysql_set_charset('utf8',$link);
  9. $mailto = 'igor.aleshin@gmail.com'; // destination email box
  10. global $license_number;
  11. $license_number = uniqid();
  12. // get tema dnya cartoon id
  13. $today = date('y-m-d');
  14. $sql = "SELECT id
  15. FROM `tema_dnya`
  16. WHERE `datetime` = '".$today."'";
  17. $result = mysql_query($sql);
  18. if (!$result) {die('<br />'.$sql.'<br />Invalid select query: ' . mysql_error());}
  19. while ($row = mysql_fetch_array($result))
  20. {
  21. $id = $row['id'];
  22. }
  23. if(isset($_GET['id']) && is_numeric($_GET['id']))
  24. {
  25. $image_id = $_GET['id']; //image to be sent to Echo //TBD
  26. }
  27. else
  28. {
  29. $image_id = $id; //default image to be sent to Echo
  30. }
  31. $sql = "SELECT wp_product_list.image as image,
  32. wp_product_list.id as ID,
  33. wp_product_list.name AS title,
  34. wp_product_brands.name AS author,
  35. wp_product_files.idhash,
  36. wp_product_list.brand AS brand,
  37. wp_product_files.mimetype
  38. FROM wp_product_list, wp_product_brands, wp_product_files
  39. WHERE
  40. wp_product_list.id = 12448
  41. AND wp_product_list.file = wp_product_files.id
  42. AND wp_product_list.brand = wp_product_brands.id";
  43. $result = mysql_query("$sql");
  44. if (!$result) {die('Invalid query: ' . mysql_error());}
  45. while($row=mysql_fetch_array($result))
  46. {
  47. $ID = $row['ID'];
  48. $image = $row['image'];
  49. $title = $row['title'];
  50. $author = $row['author'];
  51. $brand = $row['brand'];
  52. $votes = $row['votes'];
  53. $points = $row['points'];
  54. $average = $row['average'];
  55. $vote_date = $row['vote_date'];
  56. $idhash = $row['idhash'];
  57. $mimetype = $row['mimetype'];
  58. $extension = '';
  59. // get extension of the source file
  60. $fileType = strtolower(substr($mimetype, strlen($mimetype)-3));
  61. switch($fileType) {
  62. case('gif'):
  63. $extension = 'gif';
  64. break;
  65. case('png'):
  66. $extension = 'png';
  67. break;
  68. default:
  69. $extension = 'jpg';
  70. }
  71. }
  72. // save license
  73. $license_text = get_echo_license('1', '1', $title, $ID, $author);
  74. save_license($license_text);
  75. //image
  76. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/product_images/".$image;
  77. //resize the image 500
  78. $chwidth=500;
  79. $chheight=500;
  80. $thatdir='';
  81. $ifolder='';//subfolder for artist
  82. $file = $filename.'.'.$extension;
  83. $idhash_path = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/files/".$idhash;
  84. $product_images='';
  85. $slidename = 'bbb.jpg';
  86. $iconname = 'sss.jpg';
  87. $thumb='';
  88. $resample_quality=100;
  89. //create resized image
  90. al_create_resized_file($chwidth, $chheight, $thatdir, $ifolder, $file, $idhash_path, $product_images, $slidename, $thumb, $resample_quality = '100');
  91. // create icon image
  92. $imagedir = $basepath."/home/www/cb3/ales/echo/";
  93. al_create_cropped_file(200, 200, $imagedir, $ifolder, $slidename, $resample_quality = '100');
  94. // Add logo
  95. $export_dir = "/home/www/cb3/ales/echo/";
  96. if(file_exists($export_dir.$slidename))
  97. {
  98. wtrmark($export_dir.$slidename,$wm,$author.' Š cartoonbank.ru ??? echomsk.spb.ru');
  99. }
  100. //send email
  101. //email content
  102. $_link = "http://cartoonbank.ru/?page_id=29&brand=".$brand;
  103. $_link_cartoon = "http://cartoonbank.ru/?page_id=29&cartoonid=".$ID;
  104. $content = "?????: ".$author."\n\r";
  105. $content .= "????????: ".$title."\n\r";
  106. //$content .= $_link_cartoon."\n\r";
  107. $content .= "C????? ?? ???????? ? ???????????: ".$_link_cartoon."\n\r";
  108. // send email to Echo with image and license attachment
  109. //send_email_multi_attachments($content);
  110. //payment for Echo
  111. //pay_on_behalf_of_echo($image_id);
  112. mysql_close($link);
  113. ?>
  114. <?
  115. function wtrmark($sourcefile, $watermarkfile, $text) {
  116. $logopath = "/home/www/cb3/img/cb-logo-300.png";
  117. $logofile_id = imagecreatefrompng($logopath);
  118. imageAlphaBlending($logofile_id, true);
  119. imageSaveAlpha($logofile_id, true);
  120. $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));
  121. switch($fileType) {
  122. case('gif'):
  123. $sourcefile_id = imagecreatefromgif($sourcefile);
  124. break;
  125. case('png'):
  126. $sourcefile_id = imagecreatefrompng($sourcefile);
  127. break;
  128. default:
  129. $sourcefile_id = imagecreatefromjpeg($sourcefile);
  130. }
  131. imageAlphaBlending($sourcefile_id, true);
  132. imageSaveAlpha($sourcefile_id, true);
  133. //Get the sizes of both pix
  134. $sourcefile_width=imageSX($sourcefile_id);
  135. $sourcefile_height=imageSY($sourcefile_id);
  136. $logo_width=imageSX($logofile_id);
  137. $logo_height=imageSY($logofile_id);
  138. $dest_x_logo = $sourcefile_width - $logo_width - 4;
  139. $dest_y_logo = $sourcefile_height - $logo_height - 8;
  140. // if a gif, we have to upsample it to a truecolor image
  141. if($fileType == 'gif') {
  142. // create an empty truecolor container
  143. $tempimage = imagecreatetruecolor($sourcefile_width,$sourcefile_height);
  144. // copy the 8-bit gif into the truecolor image
  145. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  146. $sourcefile_width, $sourcefile_height);
  147. // copy the source_id int
  148. $sourcefile_id = $tempimage;
  149. }
  150. // create an empty truecolor container
  151. $tempimage = imagecreatetruecolor($sourcefile_width+20,$sourcefile_height);
  152. $bgColor = imagecolorallocate($tempimage, 255,255,255);
  153. imagefill($tempimage , 0,0 , $bgColor);
  154. // copy the 8-bit gif into the truecolor image
  155. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  156. $sourcefile_width, $sourcefile_height);
  157. // copy the source_id int
  158. $sourcefile_id = $tempimage;
  159. //text
  160. $black = ImageColorAllocate($sourcefile_id, 200, 200, 200);
  161. $white = ImageColorAllocate($sourcefile_id, 255, 255, 255);
  162. //The canvas's (0,0) position is the upper left corner
  163. //So this is how far down and to the right the text should start
  164. $start_x = $sourcefile_width;
  165. $start_y = $sourcefile_height;
  166. // write text
  167. Imagettftext($sourcefile_id, 10, 90, $sourcefile_width+12, $sourcefile_height-4, $black, '/home/www/cb3/ales/arial.ttf', $text);
  168. $opacity_logo = 5;
  169. ImageCopyMerge($sourcefile_id, $logofile_id, $dest_x_logo, $dest_y_logo, 0, 0, $logo_width, $logo_height, $opacity_logo);
  170. imagejpeg ($sourcefile_id,$sourcefile);
  171. //Create a jpeg out of the modified picture
  172. /*switch($fileType) {
  173. // remember we don't need gif any more, so we use only png or jpeg.
  174. // See the upsaple code immediately above to see how we handle gifs
  175. case('png'):
  176. imagepng ($sourcefile_id,$sourcefile);
  177. break;
  178. default:
  179. imagejpeg ($sourcefile_id,$sourcefile);
  180. }*/
  181. imagedestroy($sourcefile_id);
  182. imagedestroy($logofile_id);
  183. }
  184. function al_create_resized_file($chwidth, $chheight, $thatdir, $ifolder, $file, $idhash_path, $product_images, $slidename, $thumb, $resample_quality = '100') {
  185. // Default thumbs creation
  186. $img_location = $file;
  187. $export_dir = "/home/www/cb3/ales/echo/";
  188. // Creating a resource image
  189. $path = pathinfo($img_location);
  190. switch(strtolower($path["extension"])){
  191. case "pjpeg":
  192. case "jpeg":
  193. case "jpg":
  194. try
  195. {
  196. $img = imagecreatefromjpeg($idhash_path);
  197. break;
  198. }
  199. catch(Exception $e)
  200. {
  201. echo ("\n" .$idhash_path. " bad file");
  202. echo ("\n exception: " .$e);
  203. break;
  204. }
  205. case "gif":
  206. $img = imagecreatefromgif($idhash_path);
  207. break;
  208. case "png":
  209. $img = imagecreatefrompng($idhash_path);
  210. break;
  211. default:
  212. break;
  213. }
  214. $xratio = $chheight/(imagesx($img));
  215. $yratio = $chwidth/(imagesy($img));
  216. if($xratio < 1 || $yratio < 1)
  217. {
  218. if($xratio < $yratio)
  219. $resized = imagecreatetruecolor($chwidth,floor(imagesy($img)*$xratio));
  220. else
  221. $resized = imagecreatetruecolor(floor(imagesx($img)*$yratio), $chheight);
  222. imagecopyresampled($resized, $img, 0, 0, 0, 0, imagesx($resized)+1,imagesy($resized)+1,imagesx($img),imagesy($img));
  223. //echo "\n ..strtolower(path[extension]):".strtolower($path["extension"]);
  224. imagejpeg($resized, $export_dir.$slidename, $resample_quality);
  225. /*switch(strtolower($path["extension"])){
  226. case "jpeg":
  227. case "pjpeg":
  228. case "jpg":
  229. //echo "\n\r Jpg: copy resized image to ".$export_dir.$slidename;
  230. imagejpeg($resized, $export_dir.$slidename, $resample_quality);
  231. break;
  232. case "gif":
  233. //echo "\n\r Gif: copy resized image to ".$export_dir.$slidename;
  234. imagegif($resized, $export_dir.$slidename);
  235. break;
  236. case "png":
  237. imagepng($resized, $export_dir.$slidename);
  238. break;
  239. default:
  240. break;
  241. }*/
  242. imagedestroy($resized);
  243. imagedestroy($img);
  244. }
  245. else
  246. {
  247. echo "\n !!!!shit";
  248. $path["extension"] = "jpg";
  249. switch(strtolower($path["extension"])){
  250. case "jpeg":
  251. case "jpg":
  252. imagejpeg($img, $export_dir.$slidename, $resample_quality);
  253. break;
  254. case "gif":
  255. imagegif($img, $export_dir.$slidename);
  256. break;
  257. case "png":
  258. imagepng($img, $export_dir.$slidename);
  259. break;
  260. default:
  261. break;
  262. }
  263. imagedestroy($img);
  264. }
  265. }
  266. function utf8_to_cp1251($s)
  267. {
  268. if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "UTF-8")
  269. {
  270. for ($c=0;$c<strlen($s);$c++)
  271. {
  272. $i=ord($s[$c]);
  273. if ($i<=127) $out.=$s[$c];
  274. if ($byte2)
  275. {
  276. $new_c2=($c1&3)*64+($i&63);
  277. $new_c1=($c1>>2)&5;
  278. $new_i=$new_c1*256+$new_c2;
  279. if ($new_i==1025)
  280. {
  281. $out_i=168;
  282. } else {
  283. if ($new_i==1105)
  284. {
  285. $out_i=184;
  286. } else {
  287. $out_i=$new_i-848;
  288. }
  289. }
  290. $out.=chr($out_i);
  291. $byte2=false;
  292. }
  293. if (($i>>5)==6)
  294. {
  295. $c1=$i;
  296. $byte2=true;
  297. }
  298. }
  299. return $out;
  300. }
  301. else
  302. {
  303. return $s;
  304. }
  305. }
  306. function al_create_cropped_file($chwidth, $chheight, $thatdir, $ifolder, $file, $resample_quality = '85')
  307. {
  308. $img_location = $thatdir.$file;
  309. // Getting width ([0]) and height ([1]) maybe add options
  310. $size_bits = getimagesize($img_location);
  311. // Creating a resource image
  312. $path = pathinfo($img_location);
  313. $sfile = "sss.jpg";
  314. switch(strtolower($path["extension"])){
  315. case "jpeg":
  316. case "jpg":
  317. $img = imagecreatefromjpeg($img_location);
  318. $sfile = "sss.jpg";
  319. break;
  320. case "gif":
  321. $img = imagecreatefromgif($img_location);
  322. $sfile = "sss.gif";
  323. break;
  324. case "png":
  325. $img = imagecreatefrompng($img_location);
  326. $sfile = "sss.png";
  327. break;
  328. default:
  329. break;
  330. }
  331. if($size_bits[0] > $chwidth || $size_bits[1] > $chheight) {
  332. // Resize the image
  333. $resized = imagecreatetruecolor($chwidth, $chheight);
  334. $o_width = $size_bits[0];
  335. $o_height = $size_bits[1];
  336. // if the image is more wide than high
  337. if($o_width > $o_height) {
  338. // landscape image
  339. $out_width = $o_height;
  340. $out_height = $o_height;
  341. $cutoff = round(($o_width - $o_height) / 2);
  342. $out_left = $cutoff;
  343. $out_top = 0;
  344. } else {
  345. $cutoff = round(($o_height - $o_width) / 2);
  346. $out_width = $o_width;
  347. $out_height = $o_width;
  348. $out_left = 0;
  349. $out_top = $cutoff;
  350. }
  351. // Resampling the image
  352. imagecopyresampled ($resized, $img, 0, 0, $out_left, $out_top, $chwidth, $chheight, $out_width, $out_height);
  353. if (is_writable($thatdir.$ifolder)){
  354. //switch(strtolower($path["extension"])){
  355. switch("jpg"){
  356. case "jpeg":
  357. case "jpg":
  358. imagejpeg($resized, $thatdir.$ifolder.'/'.$sfile, $resample_quality);
  359. imagejpeg($resized, $thatdir.$ifolder.'/archive/'.date('y-m-d').$sfile, $resample_quality);
  360. break;
  361. case "gif":
  362. imagegif($resized, $thatdir.$ifolder.'/'.$sfile);
  363. imagegif($resized, $thatdir.$ifolder.'/archive/'.date('y-m-d').$sfile, $resample_quality);
  364. break;
  365. case "png":
  366. imagepng($resized, $thatdir.$ifolder.'/'.$sfile);
  367. imagepng($resized, $thatdir.$ifolder.'/archive/'.date('y-m-d').$sfile, $resample_quality);
  368. break;
  369. default:
  370. break;
  371. }
  372. } else {
  373. echo "<div class='error'><b>WARNING:</b> Unable to create $ifolder inside $thatdir. <br />";
  374. echo "Check your permissions.</div><br />";
  375. }
  376. imagedestroy($resized);
  377. } else {
  378. //switch(strtolower($path["extension"])){
  379. switch("jpg"){
  380. case "jpeg":
  381. case "jpg":
  382. imagejpeg($img, $thatdir.$ifolder.'/'.$file, $resample_quality);
  383. break;
  384. case "gif":
  385. imagegif($img, $thatdir.$ifolder.'/'.$file);
  386. break;
  387. case "png":
  388. imagepng($img, $thatdir.$ifolder.'/'.$file);
  389. break;
  390. default:
  391. break;
  392. }
  393. }
  394. }
  395. function send_email_multi_attachments($content='')
  396. {
  397. // array with filenames to be sent as attachment
  398. $files = array("/home/www/cb3/ales/echo/bbb.jpg","/home/www/cb3/ales/echo/license.htm");
  399. // email fields: to, from, subject, and so on
  400. $to = "igor.aleshin@gmail.com";
  401. $to_2 = "vnechay@gmail.com";
  402. $from = "cartoonbank.ru@gmail.com";
  403. $subject ="?????????? ??? ??? ?????????? ?? ???????????.??";
  404. $message = $content;
  405. $headers = "From: $from";
  406. // boundary
  407. $semi_rand = md5(time());
  408. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  409. // headers for attachment
  410. $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
  411. // multipart boundary
  412. $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
  413. $message .= "--{$mime_boundary}\n";
  414. // preparing attachments
  415. for($x=0;$x<count($files);$x++){
  416. $file = fopen($files[$x],"rb");
  417. $data = fread($file,filesize($files[$x]));
  418. fclose($file);
  419. $data = chunk_split(base64_encode($data));
  420. $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
  421. "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
  422. "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  423. $message .= "--{$mime_boundary}\n";
  424. }
  425. // send
  426. $ok = @mail($to, $subject, $message, $headers);
  427. if ($ok) {
  428. echo "<p>mail sent to $to!</p>";
  429. } else {
  430. echo "<p>mail could not be sent!</p>";
  431. }
  432. $ok = @mail($to_2, $subject, $message, $headers);
  433. if ($ok) {
  434. echo "<p>mail sent to $to!</p>";
  435. } else {
  436. echo "<p>mail could not be sent!</p>";
  437. }
  438. }
  439. function get_echo_license($sequence_of_image='1',$license_num='1', $image_name, $image_number, $author_name)
  440. {
  441. global $license_number;
  442. $agreement_date = date("m.d.y");
  443. $customer_name = "??????? ?????, Ť??? ??????????ť";
  444. $media_name = "Ť??? ??????????ť";
  445. $discount = 25;
  446. $price = 200;
  447. //load License template
  448. $filename = '';
  449. switch($license_num)
  450. {
  451. case 1:
  452. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/" . "Livense_limited_template.htm";
  453. break;
  454. case 2:
  455. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/" . "Livense_standard_template.htm";
  456. break;
  457. case 3:
  458. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/" . "Livense_extended_template.htm";
  459. break;
  460. default:
  461. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/" . "Livense_limited_template.htm";
  462. break;
  463. }
  464. $content=loadFile($filename);
  465. // replace placeholders
  466. $content = str_replace ('#agreement_number#',$license_number,$content);
  467. $content = str_replace ('#agreement_date#',$agreement_date,$content);
  468. $content = str_replace ('#customer_name#',$customer_name,$content);
  469. $content = str_replace ('#image_number#',$image_number,$content);
  470. $content = str_replace ('#image_name#',$image_name,$content);
  471. $content = str_replace ('#author_name#',$author_name,$content);
  472. $content = str_replace ('#media_name#',$media_name,$content);
  473. $content = str_replace ('#price#',$price,$content);
  474. // output content
  475. return $content;
  476. }
  477. function loadFile($sFilename, $sCharset = 'UTF-8')
  478. {
  479. if (floatval(phpversion()) >= 4.3) {
  480. $sData = file_get_contents($sFilename);
  481. } else {
  482. if (!file_exists($sFilename)) return -3;
  483. $rHandle = fopen($sFilename, 'r');
  484. if (!$rHandle) return -2;
  485. $sData = '';
  486. while(!feof($rHandle))
  487. $sData .= fread($rHandle, filesize($sFilename));
  488. fclose($rHandle);
  489. }
  490. if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset)
  491. $sData = mb_convert_encoding($sData, $sCharset, $sEncoding);
  492. return $sData;
  493. }
  494. function save_license($content)
  495. {
  496. $myFile = "/home/www/cb3/ales/echo/license.htm";
  497. $fh = fopen($myFile, 'w') or die("can't open file");
  498. $stringData = $content;
  499. fwrite($fh, $stringData);
  500. fclose($fh);
  501. }
  502. function pay_on_behalf_of_echo($cartoon_id)
  503. {
  504. // constants
  505. global $license_number;
  506. $license_num = $license_number."_".$cartoon_id;
  507. $sessionid = uniqid();
  508. $price = "250";
  509. $date_today = time();
  510. $date_download = date('y-m-d H:i:s');
  511. // wp_purchase_logs
  512. $sql = "INSERT INTO `cartoonbankru`.`wp_purchase_logs` (`id`, `totalprice`, `statusno`, `sessionid`, `transactid`, `authcode`, `user_id`, `firstname`, `lastname`, `email`, `address`, `phone`, `downloadid`, `processed`, `date`, `payment_arrived_date`, `gateway`, `shipping_country`, `shipping_region`) VALUES (NULL, '".$price."', '0', '".$sessionid."', '0', '0', '131', '???????', '?????', 'vnechay@gmail.com', '???????????', '+7(921)9341454', '0', '1', '".$date_today."', '', 'wallet', '', '')";
  513. $result = mysql_query($sql);
  514. if (!$result) {die('Invalid query: ' . mysql_error());}
  515. $purchase_id = mysql_insert_id();
  516. if (!$purchase_id) {die('Can\'t get inserted line id: ' . mysql_error());}
  517. // wp_cart_contents
  518. $sql = "INSERT INTO `cartoonbankru`.`wp_cart_contents` (`id`, `prodid`, `purchaseid`, `price`, `pnp`, `gst`, `quantity`, `license`) VALUES (NULL, '".$cartoon_id."', '".$purchase_id."', '".$price."', '0', '', '1', '".$license_num."')";
  519. $result = mysql_query($sql);
  520. if (!$result) {die('Invalid query: ' . mysql_error());}
  521. // wp_status
  522. $sql = "INSERT INTO `cartoonbankru`.`wp_download_status` (`id`, `fileid`, `purchid`, `downloads`, `active`, `datetime`) VALUES (NULL, '".$cartoon_id."', '".$purchase_id."', '0', '0', '".$date_download."')";
  523. $result = mysql_query($sql);
  524. if (!$result) {die('Invalid query: ' . mysql_error());}
  525. echo "done";
  526. }
  527. ?>