Jump to content


Photo

create image from image in database


  • Please log in to reply
6 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 May 2006 - 04:04 PM

How would i go about making a script that could put an image on coordinates but an image from a data base..

My current code is

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?php
if ($_GET['id']) {

function hexrgb ($hexstr,$img_handle)
{
$int = hexdec($hexstr);

list($r, $g, $b) = array(0xFF & ($int >> 0x10),
0xFF & ($int >> 0x8),
0xFF & $int);
return (ImageColorAllocate($img_handle, $r, $g, $b));
}


include('/var/www/profiles/library/opendb.php');
include('/var/www/countdown/countdown.php');
$id=$_GET['id'];
header ("Content-type: image/png");

$getcount=mysql_query("SELECT date, bgcolor FROM count WHERE `id` = '$id'");
$date_r=mysql_fetch_array($getcount);
$date=$date_r['date'];

list($year, $month, $day) = split("-", $row['date']);


$count_days=countdown($day,$month,$year);

$im = @ImageCreateFromPNG ("php.png");
if(!$im) {
$img_handle = ImageCreate (200, 60) or die ("Error, image not created");
$back_color = hexrgb($date_r['bgcolor'],$img_handle);
$txt_color = hexrgb($txtcolor,$img_handle);
$textquery=mysql_query("SELECT * FROM count_data WHERE countid = '$id'");
while ($text=mysql_fetch_array($textquery)) {
$txtcolor = hexrgb($text['color'],$img_handle);
ImageString ($img_handle, $text['size'], $text['countx'], $text['county'], $text['text'], $txtcolor);
}
ImagePng ($img_handle);
}
Else { }
}
else {
}
?>[/quote]


And this basicaly grabs text and the posisions from a database and displays the text where the user wants it to go.. What i also need is a way i can place an image from a database store as BLOB inside the image i generate above.

This is just a litle playaround atm..
Have a go at the text based one atm

Login with info below:
Site: [a href=\"http://www.ukchat.ws\" target=\"_blank\"]http://www.ukchat.ws[/a]
User: test
Pass: user

then click "Add Countdown" And you will see what i'm on bout and maby an idea of how it will work..

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 May 2006 - 04:31 PM

the test seems to work fine -- doing exactly what you say it should.

adding an image from a database should be relitively easy, no?

imagecreatefromstring($myrow['image']) should get it loaded. then you can just do an imagecopyresampled, or just an imagecopy to get it on your current image.

I have some thumbnail code if you need it.

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 15 May 2006 - 05:02 PM

Simple as that! :smiley:

Cheers mate!

I have some already that i use for the profiles but if u post yours too then i can choose the best :) just been coding in notepad from work for past few weeks and adding things just to make the day go by and thought this would be good idea as a novalty also goin to add img width and height so people can add then to forums and so on..

Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 15 May 2006 - 06:41 PM

I wrote this myself, but it's been tested and should be free of bugs.


<?php
$myrow=mysql_fetch_assoc(mysql_query("SELECT * FROM `table` WHERE ..."));
if (strlen($myrow['image'])>0) $im=imagecreatefromstring($myrow['image']);
else $im=imagecreatefrompng("generic.png"); // if no image, use generic image.
$width=100; //max width
$height=100; //max height
if(imagesx($im)>imagesy($im)) { //if landscape orentation
    if(imagesx($im)<=$width) { //if image is smaller than max width, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { // image is larger than maximum, resize.
        $newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width));
        imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im));
    }
} else { // image is portrait orentation (or perfectly square)
    if(imagesy($im)<=$height) { // image is smaller than max height, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { //image is larger, resize.
        $newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height);
        imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im));    
    }
}
imagedestroy($im);
?>

$newim is the starting point for whatever else you want to do.

#5 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 16 May 2006 - 10:40 AM

It's showing totaly blank mate :S

I have definatly got the data in the table as i can view the image through the site on profiles [a href=\"http://www.ukchat.ws/profiles/thumbs.php?id=5\" target=\"_blank\"]http://www.ukchat.ws/profiles/thumbs.php?id=5[/a]

code:

<?php
include('../library/header.php');
$myrow=mysql_fetch_assoc(mysql_query("SELECT * FROM `images` WHERE id = '5'"));
if (strlen($myrow['content'])>0) $im=imagecreatefromstring($myrow['content']);
else $im=imagecreatefrompng("generic.png"); // if no image, use generic image.
$width=100; //max width
$height=100; //max height
if(imagesx($im)>imagesy($im)) { //if landscape orentation
    if(imagesx($im)<=$width) { //if image is smaller than max width, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { // image is larger than maximum, resize.
        $newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width));
        imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im));
    }
} else { // image is portrait orentation (or perfectly square)
    if(imagesy($im)<=$height) { // image is smaller than max height, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { //image is larger, resize.
        $newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height);
        imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im));    
    }
}
imagedestroy($im);
?>

table:

Field name - Type
id - int(11)
name - varchar(30)
type - varchar(30)
size - int(11)
content - mediumblob
username - varchar(30)
description - varchar(254) Yes


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 16 May 2006 - 01:37 PM

Well, the code assumes you're going to do something else with the image, so there is no display. Just for grins, I've added the appropriate display lines at the bottom.

<?php
include('../library/header.php');
$myrow=mysql_fetch_assoc(mysql_query("SELECT * FROM `images` WHERE id = '5'"));
if (strlen($myrow['content'])>0) $im=imagecreatefromstring($myrow['content']);
else $im=imagecreatefrompng("generic.png"); // if no image, use generic image.
$width=100; //max width
$height=100; //max height
if(imagesx($im)>imagesy($im)) { //if landscape orentation
    if(imagesx($im)<=$width) { //if image is smaller than max width, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { // image is larger than maximum, resize.
        $newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width));
        imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im));
    }
} else { // image is portrait orentation (or perfectly square)
    if(imagesy($im)<=$height) { // image is smaller than max height, just copy it.
        $newim=imagecreatetruecolor(imagesx($im),imagesy($im));
        imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
    } else { //image is larger, resize.
        $newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height);
        imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im));    
    }
}
imagedestroy($im);
header("Content-type: image/jpeg");
imagejpeg($newim,"",90);
?>


#7 haydndup

haydndup
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationJohannesburg, South Africa

Posted 16 May 2006 - 01:56 PM

Would [a href=\"http://scudworkz.com/php_tutorials/mysql_image_storing/\" target=\"_blank\"]this[/a] be of any help? Although probably not if you can already see the images on thumbs.php
[!--coloro:#660000--][span style="color:#660000"][!--/coloro--]The Gene Pool Could Use A Little Chlorine[!--colorc--][/span][!--/colorc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users