Setting size of an image in PHP

Setting size of an image in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Quotation Mark Trap in PHP Image Attributes

When attempting to force specific dimensions on an image using PHP, a common stumbling block arises from how HTML attributes are parsed versus how PHP strings are constructed. Consider a scenario where you have a variable $media containing a URL, and you want to render it as: <img src="..." width="200" height="200">.

The intuitive approach often leads to syntax errors or broken output. If you try to concatenate the attributes directly into a single string without careful handling of quotes, the browser interprets the result incorrectly. In PHP, strings are delimited by either single quotes (') or double quotes ("). If your HTML attribute value is wrapped in double quotes, and your PHP variable uses double quotes for interpolation, you create a conflict that breaks the parsing logic before the request even reaches the client.

For example, constructing a string like "<img src=".$media."width=200 height=200"> without proper spacing or quote handling results in invalid HTML. The browser sees src= immediately followed by the variable content, but lacks the necessary separation to understand where the attribute name ends and the value begins. Furthermore, if you attempt to fix this by wrapping the values in double quotes inside a double-quoted PHP string—like "<img src=".$media." width=\"200\" height=\"200\">—you immediately hit an escaping nightmare. While technically possible, this approach is brittle and prone to errors whenever the $media variable itself contains special characters or if you need to toggle between single and double quotes dynamically.

The robust solution relies on string interpolation rules and proper delimiter selection. You must ensure that the quote character used to wrap the attribute value does not conflict with the quote character used to delimit the PHP string. A safer pattern involves using single quotes for the PHP string if the HTML attributes require double quotes, or vice versa.

Here is a clean implementation that avoids the confusion entirely:

echo "<img src='{$media}' width='200' height='200'>";

By switching to single quotes for the outer PHP string, you can use standard double quotes inside the HTML attributes without needing escape characters. This keeps the code readable and eliminates the risk of syntax errors caused by mismatched delimiters. Alternatively, if you prefer double quotes for the PHP string, you must wrap the numeric values in single quotes within the HTML:

echo "<img src=\"{$media}\" width=200 height=200>";

While modern browsers are lenient and might render the image regardless of missing quotes around numbers, adhering to valid HTML5 standards is a best practice. Validating your output ensures consistent behavior across different user agents and prevents future compatibility issues as stricter validators become more prevalent.

Another consideration involves lazy loading and performance. Explicitly setting width and height attributes allows the browser to calculate layout dimensions before the image loads, preventing Cumulative Layout Shift (CLS). This is a significant factor in Core Web Vitals scoring. However, you must ensure that these dimensions match the actual rendered size of the source file. If the $media variable points to an image that is actually 500x500 but you force it to display as 200x200 via CSS or HTML attributes without resizing the file, the browser will simply scale down the pixels. This results in a blurry image and wasted bandwidth, as the full-resolution data is still downloaded even though only a small portion is visible on screen.

If your goal is strictly to resize the image before serving it to reduce payload size, PHP alone cannot alter the pixel dimensions; you must use an imaging library like GD or ImageMagick. Simply adding HTML attributes only controls how the browser displays the existing file. Attempting to manipulate the file itself requires functions such as imagecreatetruecolor and imagecopyresampled, which are distinct from the simple attribute concatenation discussed here.

For projects using Laravel, managing asset dimensions is often handled through Blade directives or configuration rather than manual string concatenation in views. While you can still use raw PHP for dynamic image rendering, leveraging framework helpers ensures consistency with your caching and routing strategies. Always verify that your final output respects the intended aspect ratio to avoid stretching artifacts, which degrade user experience significantly.

Our network