Jump to content

Archived

This topic is now archived and is closed to further replies.

shocker-z

create image from image in database

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I wrote this myself, but it's been tested and should be free of bugs.


[code]<?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);
?>[/code]

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

Share this post


Link to post
Share on other sites
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:

[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);
?>[/code]

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

Share this post


Link to post
Share on other sites
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.

[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);
header("Content-type: image/jpeg");
imagejpeg($newim,"",90);
?>[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.