PHP: Use getimagesize to enter image dimensions in database - php

I have a number of images who's information is stored in a MySQL database. The images themselves are stored in a folder, but the database contains their URL as well as other pertinent data.
I came across the PHP getimagesize which I was thinking of using to populate the width= and height= properties of the img tags of each image. However, I have been told that the overhead for this (as there are hundreds of images) will slow down the page load substantially.
I'm curious if there is a way to use getimagesize to search out all the images from their information in the database, calculate their width and height, and then insert the data into their respective width and height fields in the database, Thus allowing a simple sql query to do the work?
Here is my database structure:
CREATE TABLE `secondary_images` (
`imgId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`primaryId` int(10) unsigned DEFAULT NULL,
`imgURL` varchar(255) DEFAULT NULL,
`width` varchar(255) DEFAULT NULL,
`height` varchar(255) DEFAULT NULL,
PRIMARY KEY (`imgId`),
KEY `primaryId` (`primaryId`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;

$image_folder = "images/";
$res = mysql_query('SELECT * FROM secondary_images')
while ($row = mysql_fetch_assoc($res)) {
$size = getimagesize($image_folder.$row['imgUrl']);
mysql_query("UPDATE secondary_images SET width='{$size[0]}', height='{$size[1]} WHERE imgId='{$row['imgId']}'");
}
Why do you want to put image size in html anyway?

This algorithm may help:
Write a query that returns the URLs and imgId of images with a width of null
For each of those images, use getimagesize() to get their image size
Write an update query to update height and width by imageid
That said, I think it would be best to skip the idea of updating when a user hits the page. At some point, the initial insertion of image data is done -- can you use getimagesize() at that time, or perhaps in a separate program which performs the scan independently of your web site?

There is no way to get size of image(s) if you don't want to use getimagesize(). I think it is fast enough when they're saved locally

Related

How to store CodeIgniter session data if data length likely to exceed BLOB size?

I have a fairly elaborate multi-page query form. Actually, my site has several for querying different data sets. As these query parameters span multiple page requests, I rely on sessions to store the accumulated query parameters. I'm concerned that the data stored in session, when serialized, might exceed the storage capacity of the MySQL BLOB storage capacity (65,535 bytes) of the data column specified by the CodeIgniter session documentation:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
How can I store my user-entered query parameters and be sure that they will be preserved for a given user?
I considered using file-based-caching to cache this data with a key generated from the session ID:
// controller method
public function my_page() {
// blah blah check POST for incoming query params and validate them
$validated_query_params = $this->input->post();
// session library is auto-loaded
// but apparently new session id generated every five mins by default?
$cache_key = "query_params_for_sess_id" . $this->session->session_id;
$this->load->driver('cache');
// cache for an hour
$this->cache->file->save($cache_key, $validated_query_params, 3600);
}
However, I worry that the session ID might change when a new session ID gets generated for a given user. Apparently this happens by default every five minutes as CodeIgniter generates new session IDs to enhance security.
Can anyone suggested a tried-and-true (and efficient!) means of storing session data that exceeds the 64K blob size?
You could use MEDIUMBLOB, which supports up to 16MB, or LONGBLOB which supports up to 4GB.
See https://dev.mysql.com/doc/refman/8.0/en/string-type-overview.html
Also, if you declare your blob with a length like BLOB(2000000) (whatever is the length you need), it will automatically promote it to a data type that can hold that length of data. For example, BLOB(2000000) will implicitly become MEDIUMBLOB.
mysql> create table t ( b blob(2000000) );
mysql> show create table t\G
*************************** 1. row ***************************
Table: tt
Create Table: CREATE TABLE `tt` (
`b` mediumblob
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

Upload image to MySQL database using mysqli

can someone provide me an example how to insert image using php mysqli into MySQL longblob field? and display it back using php mysqli?
here is my db table
IMAGE
{
IMAGE_ID int(11)
IMAGE_OWNER varchar(128)
IMAGE_BLOB longblob
IMAGE_NAME varchar(300)
IMAGE_TYPE varchar(50)
IMAGE_SIZE int(11)
}
I know how to upload it using PHP PDO, but I was wondering if I could done it using mysqli.
NOTE:
If you want to say "this is a bad way to store image" or "better use directory for storing image".
Before doing that, please tell me another way to make image only visible to it's uploader.

how do I insert images into a database?

I'm learning mysql and am having tremendous trouble with this code to build an image database....
I know how to create a table and I know I need longblob for images. Not a problem. Currently I'm creating via: CREATE TABLE pics
(
picid int unsigned not null auto_increment primary key,
filename varchar(255) not null unique,
caption varchar(255) not null,
pic longblob not null
);
the "not null" in picid is giving me problems. Because next when I attempt to populate using this code:
INSERT INTO pics values
(
NULL,
'bear.jpg',
'a picture of a bear',
LOAD_FILE('C:/Users/USERS_NAME/Pictures/bear.jpg')
);
I get hit with the error #1048 - Column 'pic' cannot be null.
please help. I am losing my mind....
It's not the picid that's the problem. LOAD_FILE('C:/Users/USERS_NAME/Pictures/bear.jpg') most likely fails and returns NULL.
Not to mention, you shouldn't store images in a database. Images are files, and should be stored as such on the file system. The database should hold the metadata + the file's address in the filesystem.
See Effeciently storing user uploaded images on the file system for a good system to follow.
if you still want to continue using the blob method try this tutorial
http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/

Images store in mysql database

I want to store images in mysql database. images sends through the android app.The problem is I really don't know which method to used to store images in msql database.
1. Access image from file
2.Store image in filesystem and store the url in the database
3.Store the image in the database
After storing these images.I want to get these images into PHP file.
I want to know which method is best and how to do that?
Yes, you can store images in the database, but it's not advisable and bad practice.
Do not store images in the database. Store images in directories and store references to the images in the database. Like store the path to the image in the database or the image name in the database.
Images can get quite large 1MB >. Even if it's a small image, it's still bad practice. You're putting extra hits on your database transactions that you can completely avoid. No big websites stores images in their database. For example: Facebook doesn't do it. It's not a good idea.
Avoid it. Use directories.
On a mobile platform database calls are expensive. Means it takes much more cycles to fetch from database and may degrade the performance of your application. Obviously you don't want that. Still if you want to do that BLOB is your answer.
The efficient way
add the absolute path field in your database and fetch the image from the local storage.
This is the best practice.
first create a photo table to store the reference of the image
CREATE TABLE IF NOT EXISTS `photos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`img` varchar(255) NOT NULL,
`sound` varchar(255) NOT NULL,
`about` text NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` varchar(255) NOT NULL,
`likes` int(255) NOT NULL,
`down` int(255) NOT NULL,
`seen` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
then in the upload process after saving the image to the folder get the image filename and store it to the table like below.
$save_image = mysql_query("INSERT INTO photos VALUES('','$originalFile','','$about','$data','$uid','0','0','0')") or die(mysql_error());
and then simply to display the image
$p = mysql_query("select img from photos");
$p_f = mysql_fetch_assoc($p);
$img = $p_f['img'];
and to display the image simply
<img src="myFolder/<?php echo $img?>">
and you done.

Compressing text before storing it in the database

I need to store a very big amount of text in mysql database. It will be millions of records with field type LONGTEXT and database size will be huge.
So, I want ask, if there is a safe way to compress text before storing it into TEXT field to save space, with ability to extract it back if needed?
Something like:
$archived_text = compress_text($huge_text);
// saving $archived_text to database here
// ...
// ...
// getting compressed text from database
$archived_text = get_text_from_db();
$huge_text = uncompress_text($archived_text);
Is there a way to do this with php or mysql? All the texts are utf-8 encoded.
UPDATE
My application is a large literature website where users can add their texts. Here is the table I have:
CREATE TABLE `book_parts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`book_id` int(11) NOT NULL,
`title` varchar(200) DEFAULT NULL,
`content` longtext,
`order_num` int(11) DEFAULT NULL,
`views` int(10) unsigned DEFAULT '0',
`add_date` datetime DEFAULT NULL,
`is_public` tinyint(3) unsigned NOT NULL DEFAULT '1',
`published_as_draft` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `key_order_num` (`order_num`),
KEY `add_date` (`add_date`),
KEY `key_book_id` (`book_id`,`is_public`,`order_num`),
CONSTRAINT FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Currently it has about 800k records and weights 4 GB, 99% of queries are SELECT. I have all reasons to think that numbers increase diagrammatically. I wouldn't like to store texts in the files because there is quite heavy logic around and my website has quite a few hits.
Are you going to index these texts. How big is read load on this texts? Insert load?
You can use InnoDB data compression - transparent and modern way. See docs for more info.
If you have realy huge texts (say, each text is above 10MB), than good idea is not to store them in Mysql. Store compressed by gzip texts in file system and only pointers and meta in mysql. You can easily expand your storage in future and move it to e.g. DFS.
Update: another plus of storing texts outside Mysql: DB stays small and fast. Minus: high probability of data inconsistence.
Update 2: if you have much programming resourses, please, take a look on projects like this one: http://code.google.com/p/mysql-filesystem-engine/.
Final Update: according to your info, you can just use InnoDB compression - it is the same as ZIP. You can start with these params:
CREATE TABLE book_parts
(...)
ENGINE=InnoDB
ROW_FORMAT=COMPRESSED
KEY_BLOCK_SIZE=8;
Later you will need to play with KEY_BLOCK_SIZE. See SHOW STATUS LIKE 'COMPRESS_OPS_OK' and SHOW STATUS LIKE 'COMPRESS_OPS'. Ratio of these two params must be close to 1.0: Docs.
If you're compressing (eg. gzip), then don't use TEXT fields of any sort. They're not binary-safe. Data going into/coming out of text fields is subject to character set translation, which probably (though not necessarily) mangle the compressed data and give you a corrupted result when you retrieve/uncompress the text.
Use BLOB fields instead, which are binary-transparent and do not to any translation of the data.
It might be better to define the text field as blob, and compress the data in PHP to save costs in communication.
CREATE TABLE book_parts (
......
content blob default NULL,
......
)
In PHP, use gzcompress and gzuncompress.
$content = '......';
$query = sprintf("replace into book_parts(content) values('%s') ",
mysql_escape_string(gzcompress($content)) );
mysql_query($query);
$query = "select * from book_parts where id = 111 ";
$result = mysql_query($query);
if ($result && $row = mysql_fetch_assoc($result))
$content = gzuncompress($row['content']);
You may also want to use a COMPRESS option to enable compression of packets.
Read some information about this option:
Use Compression in MySQL Connector/Net
Compress Property in dotConnect for MySQL
For PHP I have found this - MYSQLI_CLIENT_COMPRESS for mysqli_real_connect function.
You could use php functions gzdeflate and gzinflate for text.
There are no benefits in compressing large
texts into a database.
Here are the problems you might face in the long run:
If the server crashes the data may be hard to recover.
Not ideal for search.
It takes additional time to transfer the data between the mysql server and the browser.
Time consuming for backup (not using replication).
I think storing these large texts into a disk file will be easier for:
Distributed backup (rsync).
PHP to handle file upload.

Categories