GD blurry images Opencart - image

I have this problem with GD image processing in Opencart that creates real bad blurry images after resize. Nothing I have tried so far has helped.
Below is the code for the image.php
<?php
class Image {
private $file;
private $image;
private $info;
public function __construct($file) {
if (file_exists($file)) {
$this->file = $file;
$info = getimagesize($file);
$this->info = array(
'width' => $info[0],
'height' => $info[1],
'bits' => $info['bits'],
'mime' => $info['mime']
);
$this->image = $this->create($file);
} else {
exit('Error: Could not load image ' . $file . '!');
}
}
private function create($image) {
$mime = $this->info['mime'];
if ($mime == 'image/gif') {
return imagecreatefromgif($image);
} elseif ($mime == 'image/png') {
return imagecreatefrompng($image);
} elseif ($mime == 'image/jpeg') {
return imagecreatefromjpeg($image);
}
}
public function save($file, $quality = 100) {
$info = pathinfo($file);
$extension = strtolower($info['extension']);
if (is_resource($this->image)) {
if ($extension == 'jpeg' || $extension == 'jpg') {
imagejpeg($this->image, $file, $quality);
} elseif($extension == 'png') {
imagepng($this->image, $file);
} elseif($extension == 'gif') {
imagegif($this->image, $file);
}
imagedestroy($this->image);
}
}
/**
*
* #param width
* #param height
* #param default char [default, w, h]
* default = scale with white space,
* w = fill according to width,
* h = fill according to height
*
*/
public function resize($width = 0, $height = 0, $default = '') {
if (!$this->info['width'] || !$this->info['height']) {
return;
}
$xpos = 0;
$ypos = 0;
$scale = 1;
$scale_w = $width / $this->info['width'];
$scale_h = $height / $this->info['height'];
if ($default == 'w') {
$scale = $scale_w;
} elseif ($default == 'h'){
$scale = $scale_h;
} else {
$scale = min($scale_w, $scale_h);
}
if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png')
{
return;
}
$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, 255, 255, 255);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width,
$new_height, $this->info['width'], $this->info['height']);
imagedestroy($image_old);
$this->info['width'] = $width;
$this->info['height'] = $height;
}
public function watermark($file, $position = 'bottomright') {
$watermark = $this->create($file);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
switch($position) {
case 'topleft':
$watermark_pos_x = 0;
$watermark_pos_y = 0;
break;
case 'topright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_y = 0;
break;
case 'bottomleft':
$watermark_pos_x = 0;
$watermark_pos_y = $this->info['height'] - $watermark_height;
break;
case 'bottomright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_y = $this->info['height'] - $watermark_height;
break;
}
imagecopy($this->image, $watermark,
$watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
imagedestroy($watermark);
}
public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
$image_old = $this->image;
$this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y,
$this->info['width'], $this->info['height']);
imagedestroy($image_old);
$this->info['width'] = $bottom_x - $top_x;
$this->info['height'] = $bottom_y - $top_y;
}
public function rotate($degree, $color = 'FFFFFF') {
$rgb = $this->html2rgb($color);
$this->image = imagerotate($this->image, $degree,
imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
$this->info['width'] = imagesx($this->image);
$this->info['height'] = imagesy($this->image);
}
private function filter($filter) {
imagefilter($this->image, $filter);
}
private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
$rgb = $this->html2rgb($color);
imagestring($this->image, $size, $x, $y, $text,
imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
}
private function merge($file, $x = 0, $y = 0, $opacity = 100) {
$merge = $this->create($file);
$merge_width = imagesx($image);
$merge_height = imagesy($image);
imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width,
$merge_height, $opacity);
}
private function html2rgb($color) {
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 6) {
list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3],
$color[4] . $color[5]);
} elseif (strlen($color) == 3) {
list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1],
$color[2] . $color[2]);
} else {
return false;
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
}
?>
As you can see the quality is already set to 100 , so that doesn't help.
I have tried replacing resize with resample - but that produced no visible result.
However I have found this suggestion (code below) to sharpen images, unfortunately I am not sure how and where to use it. Especially since original code processed multiple image types. Please help to put this in the right place.
{
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
return $image;
}
Also, if you have other suggestions to improve this code, help is greatly appreciated! I think that goes for the whole Opencart community, as this has been discussed many time but no working solution posted as of yet.

The quality parameter will only be applicable to .jpeg images.
To sharpen the images you could apply the imageconvolution() code within the resize() function.
public function resize($width = 0, $height = 0, $default = '') {
if (!$this->info['width'] || !$this->info['height']) {
return;
}
$xpos = 0;
$ypos = 0;
$scale = 1;
$scale_w = $width / $this->info['width'];
$scale_h = $height / $this->info['height'];
if ($default == 'w') {
$scale = $scale_w;
} elseif ($default == 'h') {
$scale = $scale_h;
} else {
$scale = min($scale_w, $scale_h);
}
if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
return;
}
$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, 255, 255, 255);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
//Image Sharpening Code
$matrix = array(
array(0.0, -1.0, 0.0),
array(-1.0, 5.0, -1.0),
array(0.0, -1.0, 0.0)
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($this->image, $matrix, $divisor, $offset);
// End Image Sharpening Code
imagedestroy($image_old);
$this->info['width'] = $width;
$this->info['height'] = $height;
}
References:
sharpen image quality with PHP gd library
http://adamhopkinson.co.uk/blog/2010/08/26/sharpen-an-image-using-php-and-gd/

Related

laravel controller "Trying to get property 'cm_number' of non-object"

I have a problem in my controller, it doesn't get the value of what declare in getCMnumber, it needs to get the format of getCMNumber to cm_number and automatically save it to the database, but there was an error that says "Trying to get property 'cm_number' of non-object"
this my controller
public function store(Request $request)
{
try {
$userEmployment = UserEmployment::where('emp_xuser',Auth::user()->status_xuser)->first();
$stages = "UR,DH,SO,ITOPS,ITSA,ITSEC";
$cm = new ChangeManagement;
$cm->cm_user = Auth::user()->status_xuser;
$cm->cm_number = self::getCMNumber();
$cm->cm_company = $request->xcompany;
$cm->cm_projectname = $request->xproject;
$cm->cm_requesttype = $request->xcmtype;
$cm->cm_userdep = $request->xdepartment;
$cm->cm_depthead = $userEmployment->emp_xdepartment;
$cm->cm_remarks = $request->xremarks;
$cm->cm_status = "Open";
$cm->cm_priority = "Standard";
$cm->cm_stages = $stages;
$cm->cm_current = "UR";
$cm->cm_Requested = $request->xdateneeded;
$cm->save();
$dh_email = "";
$stgray = explode(',', $stages);
$stagecount = count($stgray);
$count = 0;
while($count<$stagecount) {
$cmstg_stage = $stgray[$count];
switch($cmstg_stage){
case "UR":
$cmstg_stageuser = Auth::user()->status_xuser;
break;
case "DH":
$cmstg_user = self::getdephead($userEmployment->emp_xdepartment);
$dh_email = $cmstg_user;
break;
case "SO":
$cmstg_user = self::getdephead('System Owner');
break;
case "ITOPS":
$cmstg_user = self::getdephead('IT-Operations');
break;
case "ITSA":
$cmstg_user = self::getdephead('System Administrator');
break;
case "ITSEC":
$cmstg_user = self::getdephead('IT-System Administrator');
break;
}
$cmstg = new ChangeManangementStages;
$cmstg->prstg_prnumber = $cm->cm_number;
$cmstg->prstg_stage = $cmstg_stage;
$cmstg->prstg_stageuser = $cmstg_stageuser;
$cmstg->save();
$count++;
}
$firewallcount = 0;
while ($firewallcount < count($request->pr_item)) {
$firewall = new FirewallRequest;
$firewall->cm_fw_number =$cm->cm_number;
$firewall->fw_number =self::getfirewallNumber($cm->cm_number);
$firewall->fw_user =Auth::user()->status_xuser;
$firewall->fw_stage =$stages;
$firewall->fw_purpose =$request->xpurpose[$firewallcount];
$firewall->fw_expected_result =$request->xresult[$firewallcount];
$firewall->fw_server_instance =$request->xinstance[$firewallcount];
$firewall->fw_src_servername =$request->xsrcname[$firewallcount];
$firewall->fw_src_ip =$request->xsrcip[$firewallcount];
$firewall->fw_dest_servename =$request->xdestname[$firewallcount];
$firewall->fw_dest_ip =$request->xdestip[$firewallcount];
$firewall->fw_port_services =$request->xprsr[$firewallcount];
$firewall->fw_remarks =$request->xremarks[$firewallcount];
$firewall->fw_date_requested =$request->xdatecr[$firewallcount];
$firewall->save();
$firewallcount++;
}
if($request->hasFile('filename'))
{
$path = storage_path('ChangeManagement/'.$cm->cm_number);
if(!File::exists($path)) {
Storage::disk('fw_upload')->makeDirectory($cm->cm_number);
}
$uploadcount = 0;
while ($uploadcount < count($request->filename)) {
$filename = $request->filename[$uploadcount];
$prupload = new ChangeManagementUploads;
$prupload->pr_number =$pr->pr_number;
$prupload->filename =$filename->getClientOriginalName();
$prupload->save();
Storage::disk('fw_upload')->put( '/'.$cm->cm_number.'/'.$filename->getClientOriginalName(),File::get($filename));
$uploadcount++;
}
}
alert()->success('Request submitted',$cm->cm_number.' has been submitted for review and approval')
->showConfirmButton(
$btnText = '<a class="add-padding" href="'.route('cm.show',$cm->cm_number).'">View Request</a>', // here is class for link
$btnColor = '#fa0031',
['className' => 'no-padding'],
)->autoClose(false);
return redirect()->back();
}
catch (\Throwable $th)
{
alert()->error($th->getMessage())->showConfirmButton('Return', '#fa0031');
return redirect()->back();
}
getcmNUmber
public static function getCMNumber() {
$cm = ChangeManagement::orderBy('cm_id','desc')->first();
$last_ticket = $cm->cm_number;
$tick_date = substr($last_ticket, 0, 10);
$tick_date_now = date('Y-m-d');
$tick_pre = "-CM-";
$tick_start = "000";
$tick_num = substr($last_ticket, 14, 17);
if ($tick_date == $tick_date_now) {
$tick_d1 = $tick_date;
$tick_d2 = $tick_pre;
$tick_dx = $tick_num + 1;
$tick_d3 = str_pad($tick_dx, 3, '0', STR_PAD_LEFT);
} else {
$tick_d1 = $tick_date_now;
$tick_d2 = $tick_pre;
$tick_d3 = $tick_start;
}
return $tick_dt = $tick_d1.$tick_d2.$tick_d3;
}
Just try this:
public static function getCMNumber()
{
$cm = ChangeManagement::whereNotNull('cm_number')->orderBy('cm_id', 'desc')->first();
if (!$cm) {
return null;
}
$last_ticket = $cm->cm_number;
$tick_date = substr($last_ticket, 0, 10);
$tick_date_now = date('Y-m-d');
$tick_pre = "-CM-";
$tick_start = "000";
$tick_num = substr($last_ticket, 14, 17);
if ($tick_date == $tick_date_now) {
$tick_d1 = $tick_date;
$tick_d2 = $tick_pre;
$tick_dx = $tick_num + 1;
$tick_d3 = str_pad($tick_dx, 3, '0', STR_PAD_LEFT);
} else {
$tick_d1 = $tick_date_now;
$tick_d2 = $tick_pre;
$tick_d3 = $tick_start;
}
return $tick_dt = $tick_d1 . $tick_d2 . $tick_d3;
}

Gtk2 GtkDrawingArea Cairo drawing persistence when scrolled out of range

I'm drawing on a GtkDrawingArea in a GtkScrollingArea with Cairo. First I draw a grid after the expose event, then I capture mouse signals to draw rectangles in the grid. When I scroll, the grid remains, but when I scroll out of sight of the mouse-created rectangles, they disappear, even after I scroll back into the area they were in. So for one, why does the grid remain and the rectangles do not, and what can I do about this? I could save the location of each rectangle, but in what other ways could I do this?
#!/usr/bin/perl
use strict;
use warnings;
package Gtk2::MIDIPlot;
use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;
sub new {
my $class = shift;
my $this = bless Gtk2::DrawingArea->new(), $class;
$this->signal_connect(expose_event => 'Gtk2::MIDIPlot::draw');
$this->signal_connect(button_press_event => 'Gtk2::MIDIPlot::button_press');
$this->set_events("button-press-mask");
return $this;
}
sub draw {
my $this = shift;
$this->set_size_request(28800, 1536);
my $thisCairo = Gtk2::Gdk::Cairo::Context->create($this->get_window());
$thisCairo->set_line_width(1);
$thisCairo->set_source_rgb(0.75, 0.75, 0.75);
my $inc;
for ($inc = 0; $inc <= 2400; $inc++) {
$thisCairo->move_to($inc * 12, 0);
$thisCairo->line_to($inc * 12, 1536);
};
for ($inc = 0; $inc <= 128; $inc++) {
$thisCairo->move_to(0, $inc * 12);
$thisCairo->line_to(28800, $inc * 12);
};
$thisCairo->stroke();
}
sub button_press {
my $this = shift;
my $event = shift;
if ($event->button == 1) {
my $x = $event->x;
my $y = $event->y;
my $thisCairo = Gtk2::Gdk::Cairo::Context->create($this->get_window());
$thisCairo->rectangle($x - ($x % 12), $y - ($y % 12), 12, 12);
$thisCairo->fill();
$thisCairo->stroke();
};
}
package main;
use Gtk2 -init;
my $window = Gtk2::Window->new();
my $mainWidgetScroll = Gtk2::ScrolledWindow->new();
my $mainWidget = Gtk2::MIDIPlot->new();
$mainWidgetScroll->add_with_viewport($mainWidget);
$window->add($mainWidgetScroll);
I created a global array of objects to place, and use the expose callback function to draw the objects in that array. The expose signal handler must be used for all persistent drawing.
#!/usr/bin/perl
use strict;
use warnings;
package Gtk2::MIDIPlot;
use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;
my $gtkObjects = [];
sub new {
my $class = shift;
my $this = bless Gtk2::DrawingArea->new(), $class;
$this->signal_connect(expose_event => 'Gtk2::MIDIPlot::expose');
$this->signal_connect(button_press_event => 'Gtk2::MIDIPlot::button');
$this->set_events("button-press-mask");
$this->set_size_request(28800, 1536);
return $this;
}
sub expose {
my $this = shift;
my $thisCairo = Gtk2::Gdk::Cairo::Context->create($this->get_window());
$thisCairo->set_line_width(2);
$thisCairo->set_source_rgb(0.75, 0.75, 0.75);
my $inc = 0;
for ($inc = 0; $inc <= 2400; $inc++) {
$thisCairo->move_to($inc * 12, 0);
$thisCairo->line_to($inc * 12, 1536);
};
for ($inc = 0; $inc <= 128; $inc++) {
$thisCairo->move_to(0, $inc * 12);
$thisCairo->line_to(28800, $inc * 12);
};
$thisCairo->stroke();
$thisCairo->set_source_rgb(0, 0, 0);
if(#{$gtkObjects}) {
foreach(#{$gtkObjects}) {
if(#{$_}[0] eq 'rect') {
my ($x, $y) = (#{$_}[1], #{$_}[2]);
$thisCairo->rectangle($x - ($x % 12), $y - ($y % 12), 12, 12);
$thisCairo->fill();
};
};
};
$thisCairo->stroke();
}
sub button {
my $this = shift;
my $event = shift;
if ($event->button == 1) {
my $x = $event->x;
my $y = $event->y;
push(#{$gtkObjects}, ['rect', $x, $y]);
$this->expose;
};
}
package main;
use Gtk2 -init;
my $window = Gtk2::Window->new();
my $mainWidgetScroll = Gtk2::ScrolledWindow->new();
my $mainWidget = Gtk2::MIDIPlot->new();
$mainWidgetScroll->add_with_viewport($mainWidget);
$window->add($mainWidgetScroll);

Yii:Why images are not resized correctly ?

I am using image extension for image re sizing but they are not resized according to the parameters which i gave. Here is my code.Is there any mistake in my code or what. Images which are resized have dimension equal to "800*533"
but not exactly equals to 800*600.
public function actionCreate()
{
$model=new Business;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Business']))
{
$rnd = rand(0, 9999); // generate random number between 0-9999
$model->attributes = $_POST['Business'];
$uploadedFile = CUploadedFile::getInstance($model, 'image');
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;
if ($model->save()) {
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
//$uploadedFile->saveAs(Yii::getPathOfAlias('webroot')."/img".$filename);
$uploadedFile->saveAs(Yii::app()->basePath . '/../img/' . $fileName);
$image = Yii::app()->image->load(Yii::app()->basePath . '/../img/' . $fileName);
$image->resize(800, 600);
$image->save(Yii::app()->basePath . '/../img/' . $fileName);
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
First advise. Don't store not resized image you can use
tempName property of CUploadedFile
$image = Yii::app()->image->load($uploadedFile->tempName );
$image->resize(800, 600);
$image->save(Yii::app()->basePath . '/../img/' . $fileName);
About resize i think you have to calculate size of resized picture.
Here is my code
protected static function getImgBox($img,$width,$height,$bySide,$boxType){
$img_width=$img->getSize()->getWidth();
$img_height=$img->getSize()->getHeight();
$newWidth =0;
$newHeight=0;
switch($boxType){
case self::BOX_TYPE_FILL:
{
$newWidth=$width;
$newHeight=$height;
}
break;
case self::BOX_TYPE_WO:{
if($bySide==self::BY_SIDE_WIDTH) {
$newWidth = $width;
$newHeight = $img_height * $newWidth / $img_width;
}
if($bySide==self::BY_SIDE_HEIGHT){
$newHeight=$height;
$newWidth = $img_width*$newHeight/$img_height;
}
}
break;
case self::BOX_TYPE_INSIDE:{
$newWidth = $width;
$newHeight = $img_height * $newWidth / $img_width;
if($newHeight>=$height){
$newHeight=$height;
$newWidth = $img_width*$newHeight/$img_height;
}
}
}
if($newHeight!=0 && $newWidth!=0){
return new Box(ceil($newWidth),ceil($newHeight));
}
else
return null;
}
I don't know witch extension you use. I use Imagine Extension for Yii 2
$imgpathlogo = App::param("upload_path").'outletlogo'. DIRECTORY_SEPARATOR;
$imgpathlogothumb100 = App::param("upload_path")."outletlogo". DIRECTORY_SEPARATOR."thumb100". DIRECTORY_SEPARATOR;
$imgpathlogothumb200 = App::param("upload_path")."outletlogo". DIRECTORY_SEPARATOR."thumb200". DIRECTORY_SEPARATOR;
///////////////////Chek Outlet New Logo Images////////////////
if ($_FILES['OutletMaster']['name']['outlet_logo'] != "") {
$imagelogo=$files['OutletMaster']['name']['outlet_logo'];
$logofilename=explode(".", $imagelogo);
$logofileext = $logofilename[count($logofilename) - 1];
$newlogofilename = uniqid(). "." . $logofileext;
$model->outlet_logo = $newlogofilename;
move_uploaded_file($_FILES['OutletMaster']['tmp_name']['outlet_logo'],$imgpathlogo.$newlogofilename);
//////////////////Creating Thumbnail For Outlet Logo///////////////////////////
$ext = explode(".", strtolower($newlogofilename))[1];
$src = $imgpathlogo.$newlogofilename;
if ($ext == 'gif')
$resource = imagecreatefromgif($src);
else if ($ext == 'png')
$resource = imagecreatefrompng($src);
else if ($ext == 'PNG')
$resource = imagecreatefrompng($src);
else if ($ext == 'jpg' || $ext == 'jpeg')
$resource = imagecreatefromjpeg($src);
$width = imagesx($resource);
$height = imagesy($resource);
$thumbWidth100 = 100;
$desired_width100 = $thumbWidth100;
$desired_height100 = floor( $height * ( $thumbWidth100 / $width ) );
$virtual_image = imagecreatetruecolor($desired_width100,$desired_height100);
imagecopyresized($virtual_image,$resource,0,0,0,0,$desired_width100,$desired_height100,$width,$height);
imagejpeg( $virtual_image, "{$imgpathlogothumb100}{$newlogofilename}" );
$thumbWidth200 = 200;
$desired_width200 = $thumbWidth200;
$desired_height200 = floor( $height * ( $thumbWidth200 / $width ) );
$virtual_image = imagecreatetruecolor($desired_width200,$desired_height200);
imagecopyresized($virtual_image,$resource,0,0,0,0,$desired_width200,$desired_height200,$width,$height);
imagejpeg( $virtual_image, "{$imgpathlogothumb200}{$newlogofilename}" );
}

FPDF image size

I'm using FPDF with PHP to add an image to a PDF and I want to put automatically the size of the image I find this function
function fctaffichimage($img_Src, $W_max, $H_max) {
if (file_exists($img_Src)) {
$img_size = getimagesize($img_Src);
$W_Src = $img_size[0]; // largeur source
$H_Src = $img_size[1]; // hauteur source
if(!$W_max) { $W_max = 0; }
if(!$H_max) { $H_max = 0; }
$W_test = round($W_Src * ($H_max / $H_Src));
$H_test = round($H_Src * ($W_max / $W_Src));
if($W_Src<$W_max && $H_Src<$H_max) {
$W = $W_Src;
$H = $H_Src;
} elseif($W_max==0 && $H_max==0) {
$W = $W_Src;
$H = $H_Src;
} elseif($W_max==0) {
$W = $W_test;
$H = $H_max;
} elseif($H_max==0) {
$W = $W_max;
$H = $H_test;
}
elseif($H_test > $H_max) {
$W = $W_test;
$H = $H_max;
} else {
$W = $W_max;
$H = $H_test;
}
}
}
but when i do
// requĂȘte
$tab1 = $_GET['tab1'];
$ID = $_GET['ID'];
$table = $_GET['table'];
$ree = "SELECT title,title2 FROM $tab1 WHERE $table = $ID ORDER BY 1";
$sql2 = mysql_query($ree);
// on affiche les deux images avec la fontion fctaffichimage
while ($roww = mysql_fetch_assoc($sql2))
{
$nomm = $roww["title"];
$url = "/var/www/images/".$nomm ;
fctaffichimage($url,100,100 );
$pdf->Cell(40,6,'',0,0,'C',$pdf->Image($img_Src,85,55));
}
it didn't work
I try to change the position of $url = "/var/www/images/".$nomm ;
fctaffichimage($url,100,100 ); but it didn't work too.
I see something I hope to help you. Put at the bottom of the function fctaffichimage the next code:
$img1 = imagecreatefrompng($img_Src);
$img2 = imagecreatetruecolor($W, $H);
imagecopyresampled($img2, $img1, 0, 0, 0, 0, $W, $H, $W_Src, $W_Src);
imagepng($img2, $img_Src);
Here I put a PNG image, but you can generalize it, depends of you need. It's running in my enviroment with PHP 5.3 (but there's a new imagescale function in PHP 5.5).

Session always store the previous value

I'm developing a email sending form. It has a captcha security image. When ever I refresh the image to load new captcha it always stores the previous session value. It never stores the correct one.
But in my localhost it works fine. Thank you very much.
class CaptchaSecurityImages {
var $font = 'monofont.ttf';
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
$_SESSION['securitycode'] = $code;
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$_SESSION['securitycode'] = $code;
$font_size = $height * 0.75;
$image = #imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
}
This is the class I'm using to generate captcha.

Resources