1. Add
columns has_photo, photo on jobseekers table.
2. Under
images folder, create jobseekers folder.
3. Under
application folder create helpers folder.
4. Under
helpers folder, create image_resizer.php and type the code below:
<?php
class image_resizer{
public
function __construct(){}
private
function get_file_extension($orig_image){
$filename_array=explode(".",$orig_image);//split file name into an array using the dot
$file_extension=end($filename_array);//goto at the end
$file_extension=$filename_array[1];//get file extension
return
$file_extension;
}
private
function
set_new_scale($orig_image,$orig_width,$orig_height,$width,$height,$filename){
$scale_ratio=$orig_width/$orig_height;
$scale=$width/$height;
if($scale>$scale_ratio){$width=$height*$scale_ratio;}
else{$height=$width/$scale_ratio;}
$file_extension=$this->get_file_extension($orig_image);
$new_image=$this->create_from($orig_image);
//delete
original image
unlink($orig_image);
$tci=imagecreatetruecolor($width,$height);
imagecopyresampled($tci,$new_image,0,0,0,0,$width,$height,$orig_width,$orig_height);
imagejpeg($tci,$filename,80);
}
private
function create_from($orig_image){
$file_extension=strtolower($this->get_file_extension($orig_image));
$new_image="";
if($file_extension=="gif"){$new_image=imagecreatefromgif($orig_image);}
else
if($file_extension=="png"){$new_image=imagecreatefrompng($orig_image);}
else{$new_image=imagecreatefromjpeg($orig_image);}
return
$new_image;
}
public
function resize($orig_image,$width,$height,$filename){
list($orig_width,$orig_height)=getimagesize($orig_image);
$this->set_new_scale($orig_image,$orig_width,$orig_height,$width,$height,$filename);
}
}
?>
5. Under
views folder, create jobseeker_list_view.php and type the following:
<html>
<head>
<title>Jobseeker-List | BizjobFinder.com</title>
</head>
<body>
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/application/models/jobseeker_model.php';
$obj=new jobseeker_model();
$jobseekers=$obj->get_all_jobseekers();
$ctr=0;
foreach($jobseekers as $jobseeker){
if($jobseeker['has_photo']==0){$link="Upload
Photo";?>
<div><img src="/bizjobfinder.com/images/jobseekers/no_photo.jpg"></div>
<?php }
else{ $link="Change Photo";?>
<div><img src="/bizjobfinder.com/images/jobseekers/<?php
echo $jobseeker['photo']?>"></div>
<?php }?>
<div><a href="jobseekers.php?view=jobseeker-upload-photo&id=<?php
echo $jobseeker['id']; ?>">
<?php echo $link; ?></a></div>
<div><?php
echo $jobseeker['lastname'].', '.$jobseeker['firstname'].' '.$jobseeker['mi']?></div>
<div><?php echo $jobseeker['street_barangay'].' '.$jobseeker['town_city'].','.$jobseeker['province']?></div>
<br><br>
<?php }?>
</html>
6. Under
views folder, create jobseeker_upload_photo_view.php and type the following:
<form id="frmphoto"
name="frmphoto"
enctype="multipart/form-data"
action="jobseekers.php?model=jobseeker-do-upload&id=<?php
echo $_GET['id']?>" method="post">
<br><br>
<div align="left"
class="cell_padding">
<input type='file' name='photo' /><br>
<input type="submit" id="submit" name="upload" value="Upload"
/>
</div>
</form>
7. On
jobseekers.php type the following:
<?php
session_start();
/*
*Filename:jobseekers.php
*projectname:bizjobfinder.com
*Date created:November 18,2011
*Created by:Mario T. Silvano
*/
?>
<?php
class jobseekers{
//public $value;
public function __construct(){
}
//views-----------------------------------------
public function jobseeker_list(){
return
'views/jobseeker_list_view';
}
public function upload_photo(){
return 'views/jobseeker_upload_photo_view';
}
//models---------------------------------------------
public function do_upload(){
$fileName=$_FILES["photo"]["name"];//The file name
$fileTmpLoc=$_FILES["photo"]["tmp_name"];//File in the PHP tmp Folder
$path=$_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/images/jobseekers/';
$moveResult=move_uploaded_file($fileTmpLoc,$path.$fileName);
if($moveResult!=true){
echo
"Error: File not Uploaded..Try Again";
unlink($fileTmpLoc);
exit();
}
$orig_image=$path.$fileName;
include_once($_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/application/helpers/image_resizer.php');
$img=new image_resizer();
$id=$_GET['id'];
$filename=$path.$id.".jpg";
$new_width=150;
$new_height=150;
$img->resize($orig_image,$new_width,$new_height,$filename);
$imgname=$id.".jpg";
require_once($_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/application/models/jobseeker_model.php');
$jobseeker = new
jobseeker_model();
return
$jobseeker->update_has_photo($_GET['id'],$imgname);
}
}
$obj
= new jobseekers();
$view = '';
$model = '';
if(!empty($_GET['view'])){
$view = $_GET['view'];
switch($view){
case 'jobseeker-list':
$page=$obj->jobseeker_list();
break;
case 'jobseeker-upload-photo':
$page=$obj->upload_photo();
break;
}
include
$_SERVER['DOCUMENT_ROOT'].'/bizjobfinder.com/application/'.$page.'.php';
}
else{
//models-----------------------------------------
if(!empty($_GET['model'])){
$model
= $_GET['model'];
switch($model){
case 'jobseeker-do-upload':
$result=$obj->do_upload();
header('Location:jobseekers.php?view=jobseeker-list');
break;
}
}
}
/*
*End of file jobseekers.php
*Location:/application/controllers/jobseekers.php
*/
8. On
jobseeker_model.php type the following:
<?php
class jobseeker_model{
public $id;
public $firstname;
public $mi;
public $lastname;
public $street_barangay;
public $town_city;
public $province;
public function __construct(){}
public function get_all_jobseekers(){
$sql="SELECT * FROM jobseekers
ORDER
BY lastname";
$result=$this->_execute($sql);
if($result){
$jobseekers=array();
while($r=mysql_fetch_array($result)){
$row=array();
foreach($r as
$k=>$v){
$row[$k]=$v;
}
array_push($jobseekers,$row);
unset($row);
}
}
return $jobseekers;
}
public function update_has_photo($id,$imgname){
$sql="UPDATE jobseekers set has_photo=1,
photo='{$imgname}' WHERE id='{$id}'";
return $this->_execute($sql);
}
private function _execute($sql){
include
'helpers/_dbs.php';
$db=_open_connection();
$result=mysql_query($sql,$db);
return _confirm_query($result);
}
}
?>
9. Under
models folder, create include folder.
10. Under
include folder, create _dbs.php and type the following:
<?php
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","bizjobfinder_db");
function _open_connection(){
$con
= mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$con)die('Error
Connection:'.mysql_error());
$db_select
= mysql_select_db(DB_NAME,$con);
if(!$db_select)die('Error
Selection:'.mysql_error());
return $con;
}
function _confirm_query($result){
if(!$result)die('Error
Query:'.mysql_error());
return $result;
}
?>
This is very useful tutorial, its helped me lot, i implemented this model in zend 2 but , i didn't understand what exactly these 3 lines doing
ReplyDelete$tci=imagecreatetruecolor($width,$height);
imagecopyresampled($tci,$new_image,0,0,0,0,$width,$height,$orig_width,$orig_height);
imagejpeg($tci,$filename,80);
and i am getting error at $new_image=imagecreatefrompng($orig_image); , i am passing png image only even though it is showing not a valid PNG , can you please explain this
edirne
ReplyDeletetrabzon
adana
yozgat
4K4F