OpenCart – Adjusting Captcha

This article will be a basic guide on how to change the OpenCart Captcha to something of your liking. Some people aren’t happy with the default colors that the captcha generates so we will tell you how to change those. The captcha, as you know can be found on pages like contact form, and registration. Below is a picture example of what it looks like:

captcha

You have default 3 colors for the captcha. Sometimes these colors just don’t jive with your opencart theme so we will now show you how to change them. The colors are actually generated by PHP believe it or not and the file is easily accessible. Open up the following file: /system/library/captcha.php

Once in that file locate the following function:

function showImage() {
		$image = imagecreatetruecolor($this->height, $this->width);

		$width = imagesx($image);
		$height = imagesy($image);

		$black = imagecolorallocate($image, 0, 0, 0);
		$white = imagecolorallocate($image, 255, 255, 255);
		$red = imagecolorallocatealpha($image, 86, 165, 197, 75);
		$green = imagecolorallocatealpha($image, 235, 73, 36, 75);
		$blue = imagecolorallocatealpha($image, 255, 232, 0, 75);

		imagefilledrectangle($image, 0, 0, $width, $height, $white);

		imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $red);
		imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $green);
		imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $blue);

		imagefilledrectangle($image, 0, 0, $width, 0, $black);
		imagefilledrectangle($image, $width - 1, 0, $width - 1, $height - 1, $black);
		imagefilledrectangle($image, 0, 0, 0, $height - 1, $black);
		imagefilledrectangle($image, 0, $height - 1, $width, $height - 1, $black);

		imagestring($image, 10, intval(($width - (strlen($this->code) * 9)) / 2),  intval(($height - 15) / 2), $this->code, $black);

		header('Content-type: image/jpeg');

		imagejpeg($image);

		imagedestroy($image);		
	}

This function’s job is to create a new image every time it is accessed. It is accessed every time you refresh your page on the front end (ie:user side). Get familiar with RGB. RGB is a system to create all types of colors along the spectrum. The code that you will want to change to get the new colors is here:

$black = imagecolorallocate($image, 0, 0, 0);
		$white = imagecolorallocate($image, 255, 255, 255);
		$red = imagecolorallocatealpha($image, 86, 165, 197, 75);
		$green = imagecolorallocatealpha($image, 235, 73, 36, 75);
		$blue = imagecolorallocatealpha($image, 255, 232, 0, 75);

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>